Hi,
I want to build virtual Python environment with some base packages provided by nixpkg and the rest installed by Pip (my real use case is more complicated, based on Flakes + Poetry + poetry2nix, but it behaves the same).
I can successfully create Python environment with numpy:
nix-shell -p 'python310.withPackages (p: [ p.numpy ])'
[nix-shell:~]$ which python
/nix/store/xv1vlbkpn26bg09yx5sm1ls9nkn3s8v4-python3-3.10.9-env/bin/python
[nix-shell:~]$
python -c "import numpy; print(numpy.version.full_version)"
1.23.3
but when I create Python virtualenv with --system-site-packages
enabled
python -m venv --system-site-packages venv
and activate it, I can’t access numpy anymore
[nix-shell:~]$ source venv/bin/activate
[nix-shell:~]$ python -c "import numpy; print(numpy.version.full_version)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
The problem is that virtualenv is using original Python python310
environment , not the one build with python310.withPackages
, so even with --system-site-packages
enabled, it wouldn’t find numpy, because it is not there.
- Python environment used by virtualenv
cat venv/pyvenv.cfg
home = /nix/store/65cp4izx3bllnwqn7c7dhrq9h9gmjkal-python3-3.10.9/bin
include-system-site-packages = true
version = 3.10.9
I guess the virtualenv is using sys.base_exec_prefix
to set the Python.
/nix/store/xv1vlbkpn26bg09yx5sm1ls9nkn3s8v4-python3-3.10.9-env/bin/python -c "import sys; print(sys.base_exec_prefix)"
/nix/store/65cp4izx3bllnwqn7c7dhrq9h9gmjkal-python3-3.10.9
Is there any way how to force virtualenv to use Python environment created by python310.withPackages
(python3-3.10.9-env
) ?
sys.exec_prefix
points to Python environment which I want
/nix/store/xv1vlbkpn26bg09yx5sm1ls9nkn3s8v4-python3-3.10.9-env/bin/python -c "import sys; print(sys.exec_prefix)"
/nix/store/xv1vlbkpn26bg09yx5sm1ls9nkn3s8v4-python3-3.10.9-env
Thank you