Python: fix virtualenv with Python 2

This commit is contained in:
Frederik Rietdijk
2020-05-24 09:35:45 +02:00
committed by Frederik Rietdijk
parent 98bcf5d8da
commit f17001afd8
5 changed files with 52 additions and 9 deletions

View File

@@ -21,9 +21,11 @@ paths = os.environ.pop('NIX_PYTHONPATH', None)
if paths:
functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo())
# 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
# Check whether we are in a venv or virtualenv.
# For Python 3 we check whether our `base_prefix` is different from our current `prefix`.
# For Python 2 we check whether the non-standard `real_prefix` is set.
# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
in_venv = (sys.version_info.major == 3 and sys.prefix != sys.base_prefix) or (sys.version_info.major == 2 and hasattr(sys, "real_prefix"))
if not in_venv:
executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None)
@@ -32,8 +34,6 @@ if not in_venv:
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
# Sysconfig does not like it when sys.prefix is set to None
sys.prefix = sys.exec_prefix = prefix
site.PREFIXES.insert(0, prefix)

View File

@@ -19,10 +19,8 @@ let
is_nixenv = "False";
is_virtualenv = "False";
};
} // lib.optionalAttrs (python.isPy3k && !python.isPyPy) {
} // lib.optionalAttrs (!python.isPyPy) {
# Use virtualenv from a Nix env.
# Does not function with Python 2
# ValueError: source and destination is the same /nix/store/38kz3j1a87cq5y59k5w7k9yk4cqgc5b2-python-2.7.18/lib/python2.7/os.py
nixenv-virtualenv = rec {
env = runCommand "${python.name}-virtualenv" {} ''
${pythonVirtualEnv.interpreter} -m virtualenv $out

View File

@@ -43,6 +43,10 @@ class TestCasePython(unittest.TestCase):
else:
self.assertEqual(sys.prefix, sys.base_prefix)
@unittest.skipIf(sys.version_info.major==3, "sys.real_prefix is only set by virtualenv in case of Python 2.")
def test_real_prefix(self):
self.assertTrue(hasattr(sys, "real_prefix") == IS_VIRTUALENV)
def test_python_version(self):
self.assertTrue(platform.python_version().startswith(PYTHON_VERSION))