Python packages not installing in virtualenv using pip

sav picture sav · Feb 4, 2013 · Viewed 27.4k times · Source

I'm having trouble installing twisted

pip --version

pip 1.1 from /home/chris/GL/GLBackend/glenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7)

Create a virtual environment

chris@chris-mint ~/GL/GLBackend $ sudo virtualenv -p python2.7 glenv

Running virtualenv with interpreter /usr/bin/python2.7 New python executable in glenv/bin/python2.7 Also creating executable in glenv/bin/python Installing distribute.............................................................................................................................................................................................done. Installing pip...............done.

Just in case, I'll enable all permissions

chris@chris-mint ~/GL/GLBackend $ sudo chmod -R 777 glenv

chris@chris-mint ~/GL/GLBackend $ source glenv/bin/activate


(glenv)chris@chris-mint ~/GL/GLBackend $ pip freeze

argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2

twisted is not listed here as installed

(glenv)chris@chris-mint ~/GL/GLBackend $ sudo pip install twisted

Requirement already satisfied (use --upgrade to upgrade): twisted in /usr/local/lib/python2.7/dist-packages Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/dist-packages (from twisted) Requirement already satisfied (use --upgrade to upgrade): distribute in /usr/local/lib/python2.7/dist-packages (from zope.interface>=3.6.0->twisted) Cleaning up... (glenv)chris@chris-mint ~/GL/GLBackend $ pip uninstall twisted Cannot uninstall requirement twisted, not installed Storing complete log in /home/chris/.pip/pip.log

But when I install it it says that its already installed. Force the install:

sudo pip install -I twisted

Downloading/unpacking twisted Downloading Twisted-12.3.0.tar.bz2 (2.6Mb): 2.6Mb downloaded Running setup.py egg_info for package twisted . . .

Successfully installed twisted zope.interface distribute Cleaning up...

And yet it still isn't installed

(glenv)chris@chris-mint ~/GL/GLBackend $ pip freeze

argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2

**When I try running Python scripts which use twisted, I get an error saying that twisted is not installed. That is:

ImportError: No module named twisted.python**

Answer

abarnert picture abarnert · Feb 4, 2013

The problem here is that you're using sudo when you shouldn't be. And that's causing pip to try to install into /usr/local/lib instead of ~/glenv/lib. (And, because you used sudo, it's successfully doing so, but that doesn't help you, because you're not allowing system site-packages in your venv.)

There are multiple reasons sudo pip could lead to this behavior, but the most likely is this: On most systems (including the various Mac and RHEL/CentOS boxes I have immediate access to), the sudoers file will reset your environment, then add back in a handful of whitelisted environment variables. This means that when you sudo pip, it will not see the environment variables that virtualenv sets up, so it will fall back to doing the default thing and install into your system Python, instead of your venv.

But really, it doesn't matter why this is happening. The answer is the same: just do pip install instead of sudo pip install.

Note that you also want to remove the sudo on the virtualenv call, as this will probably cause the venv to be set up incorrectly (which is why you need the sudo chmod, which wouldn't be necessary otherwise). The whole point of installing things under your user home directory is that you can do it with your normal user permissions.

As a side note, you also may want to upgrade to a newer virtualenv/pip, as 1.8 and 1.2 have some bug fixes and improvements. But I verified that I get exactly the same problem as you even with the latest (1.8.4 and 1.2.1) versions, so I don't think that's relevant here.