How can I create a python Virtual ENVironment (venv) such that the new venv has all of the python modules that are already installed on my system (with apt)?
π Note: It's much more secure to install python modules with
aptthanpip, due to the fact that pip doesn't verify the authenticity of anything that it downloads with cryptographic signatures. This means that, unlike apt (which has required authenticating everything it downloads since 2005), it is safer to obtain python modules withaptthan withpip.
Unfortunately, creating a venv in Debian 12 seems to "forget" the modules that I've already installed with apt.
user@disp3666:~$ sudo apt-get install python3-virtualenv python3-numpy
...
user@disp3666:~$
user@disp3666:~$ python3
Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy;
>>>
user@disp3666:~$
user@disp3666:~$ python3 -m virtualenv /tmp/venv
created virtual environment CPython3.11.2.final.0-64 in 336ms
creator CPython3Posix(dest=/tmp/venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/user/.local/share/virtualenv)
added seed packages: pip==23.0.1, setuptools==66.1.1, wheel==0.38.4
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
user@disp3666:~$
user@disp3666:~$ source /tmp/venv/bin/activate
(venv) user@disp3666:~$ python
Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy;
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
(venv) user@disp3666:~$
As you can see above, I've already installed numpy, but the venv can't find it.
How can I create a venv in python, such that the new virtual environment includes modules that I've already installed on the system with apt?
1 Answer 1
As described here, you can use the --system-site-packages option when creating your virtual environment to give it access to the globally installed packages:
python3 -m venv my_venv --system-site-packages
venvis to "forget" already installed modules and create empty environment (to install the same or different version, and keep this installed version even if you update version installed withapt).