Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including global package into a virtualenv that has been created with --no-site-packages

I'd usually prefer to create virtualenvs with --no-site-packages option for more isolation, and also because default python global packages includes quite a lot of packages, and usually most of them are not needed. However I'd still want to keep a few select packages in global, like PIL or psycopg2. Is there a good way to include them into the virtualenv, that can also be automated easily?

like image 867
Botond Béres Avatar asked May 04 '10 17:05

Botond Béres


People also ask

Can Python virtual environment use global packages?

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

Does virtualenv inherit packages?

If you build with virtualenv --system-site-packages ENV , your virtual environment will inherit packages from /usr/lib/python2. 7/site-packages (or wherever your global site-packages directory is).

What is the difference between VENV and virtualenv?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.


3 Answers

If you're using virtualenvwrapper and you might be able to use the postmkvirtualenv script to automatically create symlinks in the new virtualenv sitepackages directory.

#!/bin/sh
cdsitepackages
ln -s /path/to/system/site-packages/package-name
cdvirtualenv
like image 191
moberley Avatar answered Oct 28 '22 06:10

moberley


If you are using virtualenvwrapper, the shell command add2virtualenv should be present in an active virtualenv. Use:

add2virtualenv /path/to/package

to add an entry to the PTH file _virtualenv_path_extensions.pth in your virtualenv site-packages.

The benefit of using add2virtualenv rather than creating symlinks yourself, is that you can remove the package from being importable by commenting out its line in the PTH file. This makes it easier to check your code's validity against several versions of a library on which it depends.

like image 43
pcurry Avatar answered Oct 28 '22 08:10

pcurry


I haven't actually tried this with those specific packages, but I would guess that a simple symlink from the global site-packages into the virtualenv's site-packages might work, and this is easily scriptable.

like image 27
Carl Meyer Avatar answered Oct 28 '22 06:10

Carl Meyer