How to import a globally installed package to virtualenv folder

Posted on Sep 15, 2022

Question

So I have a virtualenv folder called venv for my python project.

I can run:

venv/bin/pip install -r requirements.txt

Which installs all requirements I need for the project except one, M2Crypto. The only way to install it is through apt-get:

apt-get install python-m2crypto

How can I then add this package installed through apt to venv folder?

Answer

--system-site-packages

gives access to the global site-packages modules to the virtual environment.

you could do:

$ sudo apt-get install python-m2crypto
$ virtualenv env --system-site-packages

... and you would then have access to m2crypto (along with all other system-wide installed packages) inside your virtualenv.