Skip to content

Commit

Permalink
fix(python2): do not rewrite sys.path entries for PYTHONPATH
Browse files Browse the repository at this point in the history
Fixes #1670
  • Loading branch information
jd committed Feb 26, 2020
1 parent 64d0028 commit 997d515
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/virtualenv/create/via_global_ref/builtin/python2/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def main():
prefix, exec_prefix = abs_path(sys.prefix), abs_path(sys.exec_prefix)
global_site_package_enabled = config.get("include-system-site-packages", False) == "true"
rewrite_standard_library_sys_path()
revert_pythonpath_rewrite()
disable_user_site_package()
load_host_site()
if global_site_package_enabled:
Expand Down Expand Up @@ -130,6 +131,22 @@ def rewrite_standard_library_sys_path():
sys.path[at] = translate_directory(value)


def revert_pythonpath_rewrite():
# Now that we can access os.environ["PYTHONPATH"], we can revert our
# rewrite of thoses paths done in `rewrite_standard_library_sys_path`.
import os

python_paths = os.environ.get("PYTHONPATH", "").split(":")
for ppath in python_paths:
translated_ppath = translate_directory(ppath)
try:
at = sys.path.index(translated_ppath)
except ValueError:
pass
else:
sys.path[at] = ppath


def disable_user_site_package():
"""Flip the switch on enable user site package"""
# sys.flags is a c-extension type, so we cannot monkeypatch it, replace it with a python class to flip it
Expand Down

0 comments on commit 997d515

Please sign in to comment.