35

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?

Richard Knop
  • 77,193
  • 144
  • 386
  • 546

5 Answers5

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

Corey Goldberg
  • 56,214
  • 26
  • 121
  • 139
  • As long as you are careful to only install packages you want shared globally in to the base python install, this is the way to go. – Silas Ray Dec 21 '12 at 15:14
  • But then what is the point of using a virtual environment if it has access to global site packages? – Richard Knop Dec 21 '12 at 16:03
  • 2
    Richard Knop, so you can install additional packages without root in your own virtualenv, and not mess with the system python. – Corey Goldberg Dec 21 '12 at 19:03
  • 12
    If your virtual environment is already setup and you don't want to re-create it and you're using, or open to using, _virtualenvwrapper_ to manage your virtual environments, you can add access to `--system-site-packages` ex post facto by running the [`toggleglobalsitepackages` command](http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html#toggleglobalsitepackages). – Kinsa Apr 09 '14 at 19:27
  • @RichardKnop what is the point? here is an example. i have multiple projects different libraries but i use `auto_py_to_exe` for all of them – Lucem Sep 01 '19 at 08:55
27

What I did after all:

cp -R /usr/lib/python2.7/dist-packages/M2Crypto /home/richard/hello-project/venv/lib/python2.7/site-packages/
cp -R /usr/lib/python2.7/dist-packages/OpenSSL /home/richard/hello-project/venv/lib/python2.7/site-packages/
Richard Knop
  • 77,193
  • 144
  • 386
  • 546
  • 5
    This should be built into virtual env – Sindarus Jun 15 '18 at 13:48
  • 1
    yes, I wonder if there is a virualenv method of installing into the environment the system-wide packages installed onto the host after that environment was already created. – Diego Feb 27 '19 at 15:49
20

Real simple solution.

In the virtual environment directory, edit the file pyvenv.cfg. Set the parameter include-system-site-packages = true, and save the file. The globally installed modules will appear the next time you activate (source venv/bin/activate) your environment.

It can be verified via pip list.

Enjoy!

roshnet
  • 1,216
  • 13
  • 19
  • 1
    This is the only solution I could find that did not require sudo access. My issue was that I needed to import a pre-installed global package (which had required sudo) in a virtualenv in which I wanted to install additional packages in editable mode. This wasn't possible when creating the virtualenv with `--system-site-packages`. So this helped me a lot, thank you! – Brenden Petersen May 22 '19 at 01:21
  • The appropriate solution for my scenario, which is a pretty standard one. – Alejandro QA Dec 15 '21 at 12:03
13

toggleglobalsitepackages will toggle access to the system-wide site-packages.

Note: You need to pip install virtualenvwrapper to get this command; the vanilla virtualenv doesn't include it. With virtualenvwrapper you also get the very useful mkvirtualenv and rmvirtualenv commands, among others.

Will
  • 3,770
  • 3
  • 34
  • 45
  • 1
    only if you install virtualenvwrapper. otherwise you get `toggleglobalsitepackages: command not found` – szeitlin Dec 28 '15 at 23:55
  • 3
    as of today, it is now `toggleglobalsitepackages` – eduncan911 Nov 16 '16 at 14:07
  • `virtualenvwrapper` is installed but I still get `toggleglobalsitepackages: command not found`. Those virtualenv folks really needed to design this better. Seems like a basic necessary feature IMHO. – Shailen Dec 01 '18 at 01:34
4
venv/bin/pip install -I M2Crypto

The -I forces it to also be installed into the virtualenv, even if it's already globally installed.

Amber
  • 477,764
  • 81
  • 611
  • 541
  • I will try that. Why is it not documented in output from pip --help. – Richard Knop Dec 21 '12 at 15:05
  • It's documented in `pip help install`. The `-I` flag is specific to the `install` command and thus not shown in the global flags list (there are many other command-specific flags). – Amber Dec 21 '12 at 15:06
  • 1
    Thanks. This is not working very well. You see, M2Crypto can actually be installed through pip but there is a bug in the library which makes the installation fail on Ubuntu 12.04 LTS. They released a patch but it's not working for me... So when I try your command, it tries to download the library from pip repository and install it instead of just copying the apt package. – Richard Knop Dec 21 '12 at 15:11
  • 1
    In that case, you'll probably want to use `--system-site-packages` for this one instance. – Amber Dec 21 '12 at 15:20