Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given some python package in /usr/share/pyshared/ or /usr/lib/pythonX.Y/dist-packages/ (take python-numpy or python-scipy for example), how do I add that package to a virtualenv?

When adding the package to the virtualenv, does it require that it uses the "normal" python installation method (setpy.py etc.) to install, or have it's own?

Basically, I want to be able to easily add python packages, which have C extensions that are complex (numpy, scipy, pyQt/pyside, pygtk etc.), to a virtualenv.

share|improve this question
@sr_ That's discussing the --system-site-packages option, which includes all of packages in dist-packages/site-packages. I want to only include one package. – aragilar Jun 28 '12 at 5:04

As you indicated making a wheel is often best, especially if you reinstall on a regular basis.

For python's gtk bindings this did not work for me, and I needed some way to test code using tox (which builds the virtualenvs for you with --no-site-packages). For testing purposes it would be good enough (for me) to link in the relevant system installed versions, but not everything from site-packages/dist-packages.

For this I created an installable package (ruamel.venvgtk), that sets up the proper links for Python2.7 and Python3.4 on Debian based systems (Ubuntu, Linux Mint). It does so during setup of the program, so there is no need to change your program. The tox.ini file's deps section looks like:

deps =

and for any virtualenv created pip will install ruamel.venvgtk and create the relevant links.

share|improve this answer
That's pretty cool. Personally, I hope that pgi succeeds, but I don't hold out much hope given pygir-ctypes didn't. – aragilar Dec 15 '14 at 11:32
@aragilar for Python3 I link in the gi library. I hadn't seen pgi before. I only use pygtk for a few projects, but was irritated that I couldn't test it with tox, hence the creation of that package – Anthon Dec 15 '14 at 18:33

Yes, when your virtualenv is active you need to install your required package using distutils the "normal" Python installation method (python setup.py install), or with a virtualenv aware tool (e.g. pip install package).

The version of the package that you install will be the active package version for the virtualenv. It doesn't matter whether you create the virtualenv using virtualenv MY_ENV or virtualenv --no-site-packages MY_ENV. In the first case all of the packages installed in your system Python will be available in the virtualenv, but any package that you install in the virtualenv will mask the version in your system Python while the virtualenv is active. In the second case, you will have an isolated Python environment in the virtualenv, with only the standard library packages and anything you install there available.

Be aware that packages like Numpy and Scipy don't provide pre-built binaries on Linux, so you'll need to be comfortable building them yourself if you don't want to use the versions available via your package manager.

share|improve this answer
I know how to install python packages, and have build numpy, scipy etc. previously quite a few times. I would much rather just simply use the system versions of some python libraries. The question is how do I add only some system python libraries, not all (which I've done previously also). – aragilar Jul 4 '12 at 5:56
Also, "normal" was meant cover use of distutils, setuptools/distribute, numpy.distutils, easy_install, packaging, bento etc., as opposed to path manipulation, messing with importlib and similar. – aragilar Jul 4 '12 at 6:03
If you don't want to do $ virtualenv --no-site-packages MY_ENV cd MY_ENV source bin/activate and install the packages. The only way I can see to do what you're asking is to create the virtualenv with --no-site-packages and then copy the relevant package directories from your system site-packages to the site-packages in the virtualenv.. – scottza Jul 4 '12 at 11:52
up vote 1 down vote accepted

I figure since this question hasn't had any activity for over a year (as of March 2014), no-one has an answer so I'll write how I have sort-of solved the problem.

For packages whose source install method respects virtualenvs (numpy/scipy, pyside), use wheels to avoid having to rebuild in every venv.

For packages that don't (GTK), it depends on how they hook into python.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.