222
votes

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 till 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
4
a gentle reminder -- please select an answer if one of the below appears to work. - foobarbecue
looks like 12 people have tested for you over the last few months... - foobarbecue
Why pester the OP? We can all see which is the most popular answer; does it really matter whether he accepts it? I think his integrity in testing for himself, instead of simply ticking the most popular answer, ought to be admired. - Michael Scheper
2013: Answer scores between correct and incorrect answers are pretty similar. OP says he will test. 2016: Michael Scheper is so impressed that OP (three years ago) said would test. 2017: I come across this question again and am amused. - foobarbecue
There is no correct answer. OP asked for selective --system-site-packages. Answers suggests using non-selective --system-site-packages and then overinstalling some packages locally, what is different and has different implications. - Piotr Jurkiewicz

4 Answers

254
votes

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.

19
votes

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.

-6
votes

Install virtual env with

virtualenv --system-site-packages

and use pip install -U to install matplotlib

-7
votes

You can use virtualenv --clear. which won't install any packages, then install the ones you want.