Skip to content

Python virtualenv not working because of sys.prefix mismatch #66366

Closed

Description

@Enteee

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

  1. 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

  '';
}
  1. 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

Activity

  1. added
    0.kind: bug
    on Aug 9, 2019
  2. tadfisher commented on Sep 1, 2019

    @tadfisher
    Contributor

    I was able to do a cheap workaround by copying sitecustomize.py from the system python lib dir into the virtualenv.

  3. mdedetrich commented on Oct 28, 2019

    @mdedetrich

    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.

  4. added
    6.topic: python
    0.kind: regression
    on Nov 12, 2019
  5. FRidh commented on Nov 12, 2019

    @FRidh
    Member

    This is indeed a regression.

    At some point NIX_PYTHONPATH was introduced to replace PYTHONHOME in python.buildEnv/python.withPackages (#64634). This was done so users could set PYTHONHOME themselves. Programs often use sys.executable to determine the location of the interpreter, so NIX_PYTHONEXECUTABLE was introduced that would set sys.executable to the location of the interpreter in the python.buildEnv (#65454).

    When creating a virtual environment, virtualenv will copy the Python executable which, when using python.buildEnv, will be a wrapper script.

    What can be done about this? Well, the main reason for introducing NIX_PYTHONPATH was that users could still use PYTHONHOME and add the same time unset it so it would not leak.

    Actually, we can still do that without PYTHONHOME. If we remove NIX_PYTHONPATH and NIX_PYTHONEXECUTABLE and use instead PYTHONHOME and say NIX_PYTHONHOME_UNSET where the latter would be used to control whether PYTHONHOME should 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 using

    python3.buildEnv {
      allowPythonHome = true;
    }
    

    When true, in the wrapper it would check if PYTHONHOME was set and use that, and otherwise use the Nix-generated value. In sitecustomize.py it would unset PYTHONHOME in case allowPythonHome = false;.

    cc @abbradar

  6. added this to the 20.03 milestone on Nov 12, 2019
  7. FRidh commented on Nov 12, 2019

    @FRidh
    Member

    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.

  8. self-assigned this
    on Nov 12, 2019
  9. FaustXVI commented on Nov 17, 2019

    @FaustXVI
    Contributor

    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.buildEnv section you gave.
    Could you help me ?
    Thanks

  10. sondr3 commented on Dec 9, 2019

    @sondr3
    Contributor

    I just hit this as well, makes using Nix with Python and virtualenv basically useless. Any updates?

  11. jonringer commented on Jan 14, 2020

    @jonringer
    Contributor

    @sondr3 this works on master. are you using stable (19.09)?

  12. sondr3 commented on Jan 14, 2020

    @sondr3
    Contributor

    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.

  13. andersk commented on Jan 15, 2020

    @andersk
    Contributor

    On master, python3.withPackages (ps: [ ps.virtualenv ]) still fails as described, but python3Packages.virtualenv works.

  14. myme commented on Jan 15, 2020

    @myme

    To circumvent this issue in one of our projects at $WORK, we do the following:

    shellHook = ''
      # Python wheels
      export SOURCE_DATE_EPOCH="$(date +%s)"
    
      # https://github.com/NixOS/nixpkgs/issues/66366
      export PYTHONEXECUTABLE=${venv}/bin/python
      export PYTHONPATH=${python}/lib/python3.7/site-packages
    '';
    

    This is sufficient to resolve the packages included with python37.withPackages, as well as satisfying virtualenv. Please note that it's not a solution or fix, but a temporary workaround.

    Edit: python is alias for python37.withPackages (...) and venv is just the location of the virtualenv on disk.

  15. 40 remaining items

  16. added
    2.status: stale
    on Jun 9, 2021
  17. alexvorobiev commented on Jun 16, 2021

    @alexvorobiev
    Contributor

    This is still important to me

  18. removed
    2.status: stale
    on Jun 16, 2021
  19. modified the milestones: 21.05, 23.05 on Dec 31, 2022
  20. RaitoBezarius commented on Apr 3, 2023

    @RaitoBezarius
    Member

    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)?

  21. con-f-use commented on Apr 3, 2023

    @con-f-use
    Contributor

    At this point Python 2.7 is dead, so it only has to work (if anything) on Python >3.2, Python >=3.7.0.

  22. modified the milestones: 23.05, 23.11 on May 31, 2023
  23. Snarpix commented on Jun 6, 2025

    @Snarpix

    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:

    home.packages = with pkgs; [
        (python3.withPackages (python-pkgs: [
          python-pkgs.virtualenv
        ]))
    ];
    

    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

  24. added
    2.status: stale
    on Dec 4, 2025
  25. qweered commented on May 25, 2026

    @qweered
    Contributor

    Cannot reproduce example error on current unstable, if still an issue please comment

  26. removed this from the 23.11 milestone on May 25, 2026
  27. added a commit that references this issue on Aug 5, 2026
    dc80e07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

0.kind: bug0.kind: regression2.status: stale6.topic: python

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions