File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change
1
+ Remove virtualenv `site-packages ` from `sys.path ` in context manager exit
Original file line number Diff line number Diff line change 6
6
import os
7
7
import pathlib
8
8
import shutil
9
- import site
10
9
import subprocess
11
10
import sys
12
11
import textwrap
@@ -193,11 +192,26 @@ def _add_as_extra_site_packages(self):
193
192
f"Failed to get the virtualenv's site packages path: { ret .stderr .decode ()} "
194
193
)
195
194
self .ctx .exit (1 )
196
- site_packages = site .getsitepackages ()
197
195
for path in json .loads (ret .stdout .strip ().decode ()):
198
- if path not in site_packages :
199
- site .addsitedir (path )
200
- site_packages = site .getsitepackages ()
196
+ if path not in sys .path :
197
+ sys .path .append (path )
198
+
199
+ def _remove_extra_site_packages (self ):
200
+ if self .add_as_extra_site_packages is False :
201
+ return
202
+ ret = self .run_code (
203
+ "import json,site; print(json.dumps(site.getsitepackages()))" ,
204
+ capture = True ,
205
+ check = False ,
206
+ )
207
+ if ret .returncode :
208
+ self .ctx .error (
209
+ f"Failed to get the virtualenv's site packages path: { ret .stderr .decode ()} "
210
+ )
211
+ self .ctx .exit (1 )
212
+ for path in json .loads (ret .stdout .strip ().decode ()):
213
+ if path in sys .path :
214
+ sys .path .remove (path )
201
215
202
216
def __enter__ (self ):
203
217
"""
@@ -215,6 +229,7 @@ def __exit__(self, *args):
215
229
"""
216
230
Exit the virtual environment context.
217
231
"""
232
+ self ._remove_extra_site_packages ()
218
233
219
234
def install (self , * args , ** kwargs ):
220
235
"""
You can’t perform that action at this time.
0 commit comments