Make virtualenv inherit specific packages from your global site-packages

PythonVirtualenv

Python Problem Overview


I'm looking for a way to make a virtualenv which will contain just some libraries (which I chose) of the base python installation.

To be more concrete, I'm trying to import my matplotlib to virtualenv during the creation of virtualenv. It can't be installed efficiently with pip or easy_install since it misses some fortran compiler libs. The way I did it until now was to manually copy from:

/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/

However this prevents the manully imported links to be registerd by yolk (which prints all currently available libs in virtualenv).

So, is there a way to do a selective variant of the

virtualenv --system-site-packages

Python Solutions


Solution 1 - Python

Create the environment with virtualenv --system-site-packages . Then, activate the virtualenv and when you want things installed in the virtualenv rather than the system python, use pip install --ignore-installed or pip install -I . That way pip will install what you've requested locally even though a system-wide version exists. Your python interpreter will look first in the virtualenv's package directory, so those packages should shadow the global ones.

Solution 2 - Python

You can use the --system-site-packages and then "overinstall" the specific stuff for your virtualenv. That way, everything you install into your virtualenv will be taken from there, otherwise it will be taken from your system.

Solution 3 - Python

I am late to the game using python.3.8 and pip3 on Ubuntu 20.04.

The ONLY way to get rid of the annoying .local install for me was to set an environment variable (bash):

export PYTHONNOUSERSITE="true"

This does not need to be "true" anything will work. I would not go for a 0. ;-)

Solution 4 - Python

Install virtual env with

virtualenv --system-site-packages

and use pip install -U to install matplotlib

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheMeaningfulEngineerView Question on Stackoverflow
Solution 1 - PythonfoobarbecueView Answer on Stackoverflow
Solution 2 - PythonschackiView Answer on Stackoverflow
Solution 3 - PythonStefanView Answer on Stackoverflow
Solution 4 - PythonEder MartinsView Answer on Stackoverflow