diff --git a/docs/changelog/1561.bugfix.rst b/docs/changelog/1561.bugfix.rst new file mode 100644 index 000000000..440dacbf5 --- /dev/null +++ b/docs/changelog/1561.bugfix.rst @@ -0,0 +1 @@ +Fix global site package always being added with bundled macOs python framework builds - by :user:`gaborbernat`. diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py index ff2895809..4719f68f1 100644 --- a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py +++ b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py @@ -4,6 +4,7 @@ import os import struct import subprocess +from textwrap import dedent from virtualenv.create.via_global_ref.builtin.cpython.common import CPythonPosix from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest @@ -48,6 +49,27 @@ def _executables(cls, interpreter): fixed_host_exe = Path(interpreter.prefix) / "Resources" / "Python.app" / "Contents" / "MacOS" / "Python" yield fixed_host_exe, targets + @property + def reload_code(self): + result = super(CPython2macOsFramework, self).reload_code + result = dedent( + """ + # the bundled site.py always adds the global site package if we're on python framework build, escape this + import sysconfig + + config = sysconfig.get_config_vars() + before = config["PYTHONFRAMEWORK"] + try: + config["PYTHONFRAMEWORK"] = "" + {} + finally: + config["PYTHONFRAMEWORK"] = before + """.format( + result + ) + ) + return result + def fix_mach_o(exe, current, new, max_size): """ diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py index 1f57aef67..4884f6e8d 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py @@ -33,8 +33,15 @@ def create(self): custom_site_text = custom_site.read_text() expected = json.dumps([os.path.relpath(ensure_text(str(i)), ensure_text(str(site_py))) for i in self.libs]) custom_site_text = custom_site_text.replace("___EXPECTED_SITE_PACKAGES___", expected) + custom_site_text = custom_site_text.replace( + "# ___RELOAD_CODE___", os.linesep.join(" {}".format(i) for i in self.reload_code.splitlines()).lstrip() + ) site_py.write_text(custom_site_text) + @property + def reload_code(self): + return 'reload(sys.modules["site"]) # noqa # call system site.py to setup import libraries' + @classmethod def sources(cls, interpreter): for src in super(Python2, cls).sources(interpreter): 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 44654d26e..a2ffa9c4d 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/site.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/site.py @@ -39,7 +39,7 @@ def load_host_site(): here = __file__ # the distutils.install patterns will be injected relative to this site.py, save it here - reload(sys.modules["site"]) # noqa # call system site.py to setup import libraries + # ___RELOAD_CODE___ # and then if the distutils site packages are not on the sys.path we add them via add_site_dir; note we must add # them by invoking add_site_dir to trigger the processing of pth files