From 997d515d7dcf4b61056f136f19d31fd5a1bf044b Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 26 Feb 2020 17:17:28 +0100 Subject: [PATCH] fix(python2): do not rewrite sys.path entries for PYTHONPATH Fixes #1670 --- .../via_global_ref/builtin/python2/site.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/site.py b/src/virtualenv/create/via_global_ref/builtin/python2/site.py index 839fadbbf..59f722c40 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/site.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/site.py @@ -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: @@ -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