61

Possible Duplicate:
Make virtualenv inherit specific packages from your global site-packages

Is there a way to create a virtualenv for Python and specify which packages should be used (inherited) from the system-wide installation, and which ones it should ignored from the system-wide installation?

More specifically, say for example that there is a system-wide installation of:

numpy
scipy
matplotlib

I would like to create a virtual environment such that:

  • Uses the system-wide installation of numpy and scipy
  • Ignores the system-wide matplotlib, and lets me install/upgrade my own versions of it (with pip -U matplotlib).

Is this possible?

Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    you can upgrade packages in a virtualenv even if they are first included from the system-wide installation (if this is the only reason for your question). – bmu Jan 28 '13 at 21:26
  • Thanks @bmu. How can you do that? Whenever I try `pip install -U matplotlib` it tries to upgrade the system-wide installation. And, assuming that I can upgrade a package that was already installed system-wide only within the virtualenv, how will it know later which one to use? – Amelio Vazquez-Reina Jan 28 '13 at 21:30
  • 1
    first create the virtualenv, activate it, then install the package. it should be installed to your virtualenv, not in your system-wide installation. – bmu Jan 28 '13 at 21:33
  • 2
    virtualenv first tries to find the package locally, then system wide. – bmu Jan 28 '13 at 21:37
  • 1
    According to [this issue](https://github.com/pypa/virtualenv/issues/369), with current(2014) ``virtualenv`` you CANNOT upgrade a system-inherited package because system-paths are ordered BEFORE virtualenv-paths within ``sys.path``! – ankostis Sep 10 '14 at 14:39
  • 1
    Not exactly a duplicate of the other question, because the answers there all leave out the **which packages** aspect. – bluenote10 Oct 17 '18 at 12:17

1 Answers1

87

The simplest way to do this is to create a virtualenv which includes the system site packages and then install the versions that you need:

$ virtualenv --system-site-packages foo
$ source foo/bin/activate
$ pip install Django==1.4.3

You can also clean up the virtualenv afterwards by checking the output of pip freeze and removing the packages that you do not want. (removing system-site-packages with pip uninstall does no longer work for newer versions of virtualenv)

Another way would be to create a clean virtualenv and link the parts that you need:

$ virtualenv --no-site-packages foo
$ source foo/bin/activate
$ ln -s /usr/lib/python2.7/dist-packages/PIL* $VIRTUAL_ENV/lib/python*/site-packages

The commands might be slightly different on a non-unixish environment. The paths also depend on the system you are using. In order to find out the path to the library start up the python shell (without an activated virtualenv), import the module and check module_name.__path__. e.g.

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__path__
['/usr/lib/python2.7/dist-packages/PIL']

Also if you have created your virtualenv with --system-site-packages, it is possible to install newer version than what is in the system with pip install --upgrade --ignore-installed numpy.

jhutar
  • 1,369
  • 2
  • 17
  • 32
bikeshedder
  • 7,337
  • 1
  • 23
  • 29
  • 1
    Depending on your virtualenv version you might no longer be able to uninstall system packages which are inside your virtualenv because of --system-site-packages. In recent virtualenv versions the site-packages directory is no longer a collection of links to your system wide packages. Therefore you can only install other versions of a package but not remove it. I would always recommend using a clean virtualenv and only linking the system packages that you need unless you are in a hurry. – bikeshedder Jan 28 '13 at 21:39
  • 7
    The symbolic link works, but if the package has other dependencies, you'll have to go hunting all those down and linking them too. – Permafacture Jul 14 '14 at 20:00
  • Could you explain what each of these commands do and their expected output, particularly `source`? I have an existing virtualenv and I want `import pylab` to work in it, like it does in my system python environment (without virtualenv). After I follow your first set of steps, replacing 'foo' with 'pylab', I see 'Could not find any downloads that satisfy the requirement pylab'. And yet, `import pylab` works, and `inspect.getfile(pylab)` points to the same location as in the system python. But then, when I activate the same virtualenv in another shell, `import pylab` still doesn't work. – Michael Scheper Oct 04 '16 at 17:44
  • Creating a new virtual environment doesn't automatically inherits the systemwide packages? I thought it did – JobHunter69 Jun 08 '19 at 20:34