Make Virtualenv Inherit Specific Packages from Your Global Site-Packages

What does it mean to Inherit global site-packages in PyCharm?

It's just an option to pre-install some packages that you're using everytime, or if it doesn't bother you to have extra packages in your local python interpreted

  • select it : all packages installed in the global python of your machine will be installed for the interpreter you're going to create in the virtualenv.
  • do not select it : the interpreter you're going to create in the virtualenv will just have the basic, like pip, and setuptools, then you can install just what you need

Python global and venv :

  • The global python, is the one in /usr/bin in Linux, or wherever in Windows, this is the main installation of the program, and you can add extra packages using pip

  • When you're working on something, you may need only some packages, or specific version so not using the global Python. You can create a virtualenv, or pyenv, that will link a local python to the global one, for the main python functionnality, but the packages will be installed only in the virtualenv (and when using Pycharm, it can install for you the main package into the virtualenv you're creating)

Make virtualenv share already existing site-packages?

Scrap the comment, maybe I do know an answer. It appears as though Anaconda's package management system does use symlinks. So that would basically be a virtualenv but with the feature you want. See here: How to free disk space taken up by (ana)conda?

That said, there's a large initial harddisk cost to using Conda, so investigate a bit more and decide if it will work for you.

How to import a globally installed package to virtualenv folder

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/

Installing local packages with Python virtualenv --system-site-packages

Create the virtual environment without the --system-site-packages switch. After the environment was created go to the folder the environment was created in. It should have a file pyvenv.cfg. Edit this file. It has (among other text) a line

include-system-site-packages = false

Change this line to:

include-system-site-packages = true

Activate the environment. Module installations will now go to the virtual environment and the system site packages are visible too.

virtualenv: Specifing which packages to use system-wide vs local

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.



Related Topics



Leave a reply



Submit