Repository navigation
Closed
Labels
0.kind: bugSomething is broken0.kind: regressionSomething that worked before working no longer2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: pythonPython is a high-level, general-purpose programming language.
Description
Describe the bug
Virtualenvwrapper in current nixpkgs-unstable exits with the following error when creating a new virtual environment.
ERROR: The executable /tmp/venv/bin/python3.7 is not functioning
ERROR: It thinks sys.prefix is '/nix/store/zjzq4qp81z8qnnnchnkycmsq0impw173-python3-3.7.4' (should be '/tmp/venv')
ERROR: virtualenv is not compatible with this system or executable
This might be intended behavior, maybe because of this PR, but then this section of the wiki probably needs a bit of attention.
To Reproduce
- write
default.nix
with import (builtins.fetchGit {
# Descriptive name to make the store path easier to identify
name = "nixpkgs-fixed-rev";
url = https://github.com/NixOS/nixpkgs-channels/;
# current unstable:
rev = "002b853782e939c50da3fa7d424b08346f39eb6f";
# 19.03 release:
#ref = "nixos-19.03"; # 56d94c8c69f8cac518027d191e2f8de678b56088
}) {};
let
python-with-base-packages = python3.withPackages(ps: with ps; [
ipython
virtualenvwrapper
# the following packages are related to the dependencies of your python
# project.
# In this particular example the python modules listed in the
# requirements.txt require the following packages to be installed locally
# in order to compile any binary extensions they may require.
#
]);
in stdenv.mkDerivation {
name = "virtualenvwarpper-test";
src = null;
buildInputs = [
python-with-base-packages
];
shellHook = ''
# set SOURCE_DATE_EPOCH so that we can use python wheels
SOURCE_DATE_EPOCH=$(date +%s)
set -x
if [ ! -e venv/bin/activate ]; then
virtualenv --system-site-packages --clear venv
fi
source venv/bin/activate
export PATH="''${PATH}:venv/bin"
if [ -e requirements.txt ]; then
pip install -r requirements.txt
fi
set +x
'';
}nix-shell
Expected behavior
The behavior virtualenv shows when switching in the default.nix posted above to the 19.03 release.
Metadata
- system:
"x86_64-linux" - host os:
Linux 4.14.136, NixOS, 19.09pre188002.e275a6c2f38 (Loris) - multi-user?:
yes - sandbox:
yes - version:
nix-env (Nix) 2.2.2 - channels(root):
"nixos-19.09pre188002.e275a6c2f38, nixos-hardware, nixpkgs-19.09pre187222.002b853782e" - nixpkgs:
/nix/var/nix/profiles/per-user/root/channels/nixos
Reacted by Tadeo Kondrak, Matthew de Detrich, Ryan J.P. Casalino, Gabriel Adomnicai, other-mickk, Nils Andre, Stefano Mazzucco, bb010g, coderfromhere, ThoenigAdrian and 6 more
Metadata
Metadata
Assignees
Labels
0.kind: bugSomething is broken0.kind: regressionSomething that worked before working no longer2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: pythonPython is a high-level, general-purpose programming language.
Activity
tadfisher commentedon Sep 1, 2019
I was able to do a cheap workaround by copying
sitecustomize.pyfrom the system python lib dir into the virtualenv.mdedetrich commentedon Oct 28, 2019
I am also getting this issue, @tadfisher can you explain your workaround in more details? I tried doing this but I couldn't get it to work.
FRidh commentedon Nov 12, 2019
This is indeed a regression.
At some point
NIX_PYTHONPATHwas introduced to replacePYTHONHOMEinpython.buildEnv/python.withPackages(#64634). This was done so users could setPYTHONHOMEthemselves. Programs often usesys.executableto determine the location of the interpreter, soNIX_PYTHONEXECUTABLEwas introduced that would setsys.executableto the location of the interpreter in thepython.buildEnv(#65454).When creating a virtual environment,
virtualenvwill copy the Python executable which, when usingpython.buildEnv, will be a wrapper script.What can be done about this? Well, the main reason for introducing
NIX_PYTHONPATHwas that users could still usePYTHONHOMEand add the same time unset it so it would not leak.Actually, we can still do that without
PYTHONHOME. If we removeNIX_PYTHONPATHandNIX_PYTHONEXECUTABLEand use insteadPYTHONHOMEand sayNIX_PYTHONHOME_UNSETwhere the latter would be used to control whetherPYTHONHOMEshould be unset (by default on), then we achieve the same, however, without regressions. Users that want to use virtualenv, would then create a Nix env first usingWhen true, in the wrapper it would check if
PYTHONHOMEwas set and use that, and otherwise use the Nix-generated value. Insitecustomize.pyit would unsetPYTHONHOMEin caseallowPythonHome = false;.cc @abbradar
FRidh commentedon Nov 12, 2019
Even better, in case of Python 3 we should just use
pyvenv.cfg. Before I never considered it an option because we had to support 2 and 3, but now, with Python 2 going EOL, I think we should just take that route and leave a "best-effort" alternative for Python 2.FaustXVI commentedon Nov 17, 2019
Hi, I am encountering this issue and don't understand the workaround you gave. In particular, I don't understand where to put the
python3.buildEnvsection you gave.Could you help me ?
Thanks
sondr3 commentedon Dec 9, 2019
I just hit this as well, makes using Nix with Python and virtualenv basically useless. Any updates?
jonringer commentedon Jan 14, 2020
@sondr3 this works on master. are you using stable (19.09)?
sondr3 commentedon Jan 14, 2020
I gave up on writing the project in Python/Nix/virtualenv and converted it to Rust so I won't be able to check it, but yes, at the time I was using
nixos-19.09.andersk commentedon Jan 15, 2020
On master,
python3.withPackages (ps: [ ps.virtualenv ])still fails as described, butpython3Packages.virtualenvworks.myme commentedon Jan 15, 2020
To circumvent this issue in one of our projects at $WORK, we do the following:
This is sufficient to resolve the packages included with
python37.withPackages, as well as satisfyingvirtualenv. Please note that it's not a solution or fix, but a temporary workaround.Edit:
pythonis alias forpython37.withPackages (...)andvenvis just the location of the virtualenv on disk.40 remaining items
alexvorobiev commentedon Jun 16, 2021
This is still important to me
RaitoBezarius commentedon Apr 3, 2023
Could we have a status report on the current state of this issue? Working on Python ≥ 3, broken on Python 2.7, not working with some programs (pre-commit)?
con-f-use commentedon Apr 3, 2023
At this point Python 2.7 is dead, so it only has to work (if anything) on Python >3.2, Python >=3.7.0.
Snarpix commentedon Jun 6, 2025
So what is the current workaround for this issue?
According to python docs, sys.prefix != sys.base_prefix is the way to check for python venv.
And when I run
python -c "import sys; print(sys.prefix != sys.base_prefix)"I get True on a system with python installed via home manager:I am trying to run vscode ESP-IDF extension on nixos, and the extension checks for venv like it's told to by docs, so I can't blame the extension=)
To be more precise:
➜ ~ nix-shell --pure -p python3 --run 'python3 -c "import sys; print(sys.prefix != sys.base_prefix);"'
False
➜ ~ nix-shell --pure -p 'python3.withPackages(p: [])' --run 'python3 -c "import sys; print(sys.prefix != sys.base_prefix);"'
True
qweered commentedon May 25, 2026
Cannot reproduce example error on current unstable, if still an issue please comment
pythonPackages.virtualenv: 16.7.9 -> 20.0.21 fix NixOS#66366