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 27, 2020
1 parent 45622e9 commit 56fc536
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 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 @@ -20,6 +20,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(base_executable, exe_dir, exec_prefix, base_prefix, prefix, base_exec_prefix)
revert_python_path_rewrite(base_executable, exe_dir, exec_prefix, base_prefix, prefix, base_exec_prefix)
disable_user_site_package()
load_host_site()
if global_site_package_enabled:
Expand Down Expand Up @@ -124,6 +125,24 @@ def translate_directory(directory, base_executable, exe_dir, exec_prefix, base_p
return directory


def revert_python_path_rewrite(base_executable, exe_dir, exec_prefix, base_prefix, prefix, base_exec_prefix):
# 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 map(abs_path, python_paths):
translated_ppath = translate_directory(
ppath, base_executable, exe_dir, exec_prefix, base_prefix, prefix, base_exec_prefix
)
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 56fc536

Please sign in to comment.