From 7447fff95a4e091dc9ebe961d46cb4fbc32e69d1 Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Sat, 14 Mar 2020 16:22:36 +0100 Subject: [PATCH] Fix sys.prefix in case of a Nix env The prefix will now be correct in case of Nix env. Note, however, that creating a venv from a Nix env still does not function. This does not seem to be possible with the current approach either, because venv will copy or symlink our Python wrapper. In case it symlinks (the default) it won't see a pyvenv.cfg. If it is copied I think it should function but it does not... --- .../interpreters/python/sitecustomize.py | 21 +++++++++++++------ .../interpreters/python/tests/test_python.py | 4 ++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pkgs/development/interpreters/python/sitecustomize.py b/pkgs/development/interpreters/python/sitecustomize.py index e59d6070cb6..72ce951328f 100644 --- a/pkgs/development/interpreters/python/sitecustomize.py +++ b/pkgs/development/interpreters/python/sitecustomize.py @@ -21,10 +21,19 @@ paths = os.environ.pop('NIX_PYTHONPATH', None) if paths: functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo()) -prefixes = os.environ.pop('NIX_PYTHONPREFIX', None) -if prefixes: - site.PREFIXES.extend(prefixes.split(':')) +# Check whether we are in a venv. +# Note Python 2 does not support base_prefix so we assume we are not in a venv. +in_venv = sys.version_info.major == 3 and sys.prefix != sys.base_prefix -executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None) -if 'PYTHONEXECUTABLE' not in os.environ and executable: - sys.executable = executable +if not in_venv: + executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None) + prefix = os.environ.pop('NIX_PYTHONPREFIX', None) + + if 'PYTHONEXECUTABLE' not in os.environ and executable is not None: + sys.executable = executable + if prefix is not None: + # Because we cannot check with Python 2 whether we are in a venv, + # creating a venv from a Nix env won't work as well with Python 2. + # Also, note that sysconfig does not like it when sys.prefix is set to None + sys.prefix = sys.exec_prefix = prefix + site.PREFIXES.insert(0, prefix) diff --git a/pkgs/development/interpreters/python/tests/test_python.py b/pkgs/development/interpreters/python/tests/test_python.py index 93aa2bb2b9e..011978c6254 100644 --- a/pkgs/development/interpreters/python/tests/test_python.py +++ b/pkgs/development/interpreters/python/tests/test_python.py @@ -27,7 +27,7 @@ class TestCasePython(unittest.TestCase): def test_interpreter(self): self.assertEqual(sys.executable, INTERPRETER) - @unittest.skipIf(IS_NIXENV or IS_PYPY, "Prefix is incorrect and needs to be fixed.") + @unittest.skipIf(IS_PYPY, "Prefix is incorrect and needs to be fixed.") def test_prefix(self): self.assertEqual(sys.prefix, ENV) self.assertEqual(sys.prefix, sys.exec_prefix) @@ -37,7 +37,7 @@ class TestCasePython(unittest.TestCase): @unittest.skipIf(IS_PYPY or sys.version_info.major==2, "Python 2 does not have base_prefix") def test_base_prefix(self): - if IS_VENV: + if IS_VENV or IS_NIXENV: self.assertNotEqual(sys.prefix, sys.base_prefix) else: self.assertEqual(sys.prefix, sys.base_prefix)