How do I get virtualenv to prefer its local libraries over global libraries

Question:

I need to use python on a big server where I don’t have root access. I want to use a newer version of numpy than the one that is installed globally on the machine. virtualenv is designed exactly for this purpose, and I create my virtual environment and activate it with the following commands:

virtualenv my_personal_python
source my_personal_python/bin/activate

I then install the new version of the library that I’m interested in using

pip install numpy==1.6.0

The problem is that when I now import numpy it still imports the outdated global version, not the one install in the virtual environment’s my_personal_python/lib/python2.6/site-packacges directory.

I am already aware of one possible solution, the –no-site-packages flag, as in:

virtualenv --no-site-packages my_personal_python

When I use this flag then the import behaves as I desire. But I don’t want to use this flag because I do not want to re-install all packages locally, I just want to override a couple of them.

(I’m using python 2.6, virtualenv 1.6.1, and the PYTHONPATH variable on my machine is not set.)

Update Even if I add the site-packages directory from the virtual environment to the beginning of the python path, numpy does not get imported from this location (although other packages are imported from this location). Maybe this problem is specific to numpy and does not occur with packages in general.

Asked By: conradlee

||

Answers:

Double check a few things.

which python

which pip

Now that you are sure you are running the right one, start python and:

import sys
print "n".join(sys.path)

Then exit python and type echo $PATH followed by echo $PYTHONPATH
I suspect that the problem will be visible and if you cannot fix it by setting PYTHONPATH then you can likely do it with the site module.

Answered By: Michael Dillon

This worked for me.

My which python and which pip were exactly correct but the sys.path was wrong. My virtualenv is in ~/virtualenvs/envy. Originally I was doing:

export PYTHONPATH=~/virtualenvs/envy/lib/python2.7/site-packages:$PYTHONPATH

but this still was importing the system-wide package instead of my virtualenv one. BUT I watched this PyCon talk on virtualenv and decided to try:

export PYTHONPATH=~/virtualenvs/envy/lib/python2.7:$PYTHONPATH

Notice the lack of site-packages in the second option. And this actually worked! I hope it helps someone else.

Answered By: Olga Botvinnik

One more solution to this problem (helped me, at least): In my ~/.local/lib/python2.7/site-packages/easy-install.pth, there were (IMHO unnecessary) lines like /usr/lib/python2.7/dist-packages. Removing these lines helped, maybe they were left over from much older times, when easy_install still did stranger things.

Answered By: quazgar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.