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 66import os
77import pathlib
88import shutil
9- import site
109import subprocess
1110import sys
1211import textwrap
@@ -193,11 +192,26 @@ def _add_as_extra_site_packages(self):
193192 f"Failed to get the virtualenv's site packages path: { ret .stderr .decode ()} "
194193 )
195194 self .ctx .exit (1 )
196- site_packages = site .getsitepackages ()
197195 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 )
201215
202216 def __enter__ (self ):
203217 """
@@ -215,6 +229,7 @@ def __exit__(self, *args):
215229 """
216230 Exit the virtual environment context.
217231 """
232+ self ._remove_extra_site_packages ()
218233
219234 def install (self , * args , ** kwargs ):
220235 """
You can’t perform that action at this time.
0 commit comments