virtualenv does not include pip

Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg

I’m putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!

I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip from within the virtual environment installed to the global package location, and was not visible to the environment’s python.

How I diagnosed this on my machine:

  1. I create a virtualenvironment with $ virtualenv env
  2. Activated the virtual environment with $ source env/bin/activate
  3. Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected)
  4. Checked pip location: run (env)$ which pip with output /usr/local/bin/pip (NOT expected)

To check where our packages are going, we can try to install a package in the virtual environment:

  1. Try to install a package: (env)$ pip install HTTPServer which succeeds
  2. Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
  3. To double-check, try to install again: (env)$ pip install HTTPServer which produces Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages

Double-checking, we see that there’s no Pip in the environment’s /bin folder:

$ ls env/bin
activate activate.fish python python2
activate.csh activate_this.py python-config python2.7

And so while the system finds the local python version, it can’t find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.

Here’s what I tried:
– Reinstalling python brew uninstall python followed by brew upgrade and brew install python --build-from-source
– Installing pip using the get-pip.py command as described in the Pip documentation

Here’s what I ruled out:
– I was not using sudo pip ... which caused similar problems in this other question and haven’t done so at any time on this Python/pip install
– My virtual environment didn’t show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.

Ultimately, I found that eliminating the ~/.pydistutils.cfg file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg file were:

[global]
verbose=1

[install]
install-scripts=$HOME/bin

[easy_install]
install-scripts=$HOME/bin

Simply renaming the ~/.pydistutils.cfg file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn’t had any bad effects on my system, you may need to use the --user flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.

Leave a Comment