1

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 apt than pip, 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 with apt than with pip.

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?

David Buck
3,88640 gold badges54 silver badges74 bronze badges
asked Oct 6, 2025 at 21:29
2
  • one of idea of venv is 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 with apt). Commented Oct 7, 2025 at 13:02
  • does my answer solve your question? I think it should but let me know if I misunderstood your question Commented Oct 8, 2025 at 1:52

1 Answer 1

3

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
answered Oct 7, 2025 at 0:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.