virtualenv does not include pip

Question:

I am trying to create a virtual environment using virtualenv on Mac OS X El Capitan. I have installed Python 2.7.11 with brew, which includes pip, wheel and setuptools by default.

Hovewer, when I try to install virtualenv following instructions in the documentation or from any other resource, I get several problems:

  1. virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
  2. After I run virtualenv venv, it creates new environment, catches Python 2.7.11 from brew-installation, but: there is no pip inside bin folder. That means, that if I try which pip, having venv activated, it returns a global position of pip/usr/local/bin/pip, not /path/to/venv/bin/pip.

As a consequence, installing packages inside venv uses global pip and installs them to a global sites-packages, not that inside venv, and it’s quite the opposite of what environment should do.

Could you please suggest what may be wrong and how to fix it?

EDIT: The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.

Asked By: mikeonly

||

Answers:

  1. virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).

Don’t do that. That will only hide the bug and not solve the problem. Here’s a short guide how to debug this kind of issues:

  • Start with which -a python. The first path you see should be /usr/local/bin/python, if not check your PATH variable.

  • Next, check which -a pip. Again the first path should be /usr/local/bin/pip. If not, run python -m ensurepip and recheck.

  • Now install virtualenv using pip install virtualenv, after that check the output of which -a virtualenv. The first path should be /usr/local/bin/virtualenv, if not check the output of env |grep PYTHON for unexpected environment variables.

  • Finally check the output of virtualenv --version to make sure you have the latest version.

Answered By: cel

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.

Answered By: emunsing

I had the issue when running virtualenv: “ImportError: No module named pip.”
My solution was to downgrade virtualenv. I had 16.2.0.
pip uninstall virtualenv
pip install virtualenv==15.1.0

Answered By: Nazar

Just hit same issue on Linux. Seems like there are multiple causes of this issue, but for me answer was to remove ~/.pip/.

Details: I had this in my .pip/pip.conf for some reason I can’t remember:

$ cat ~/.pip/pip.conf 
[global]
use_https = True 
index = https://pypi.python.org/pypi
prefix = /home/sam/local/

and was using local versions on Python, Pip installed at ~/local/. For some reason virtualenv installed pip must pick up prefix = /home/sam/local/ setting and pip was being installed there.

Answered By: spinkus

Try this: sudo apt install pythonV.v-distutils.
In my case V.v == 3.8.
This worked for me.

Answered By: MS MS
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.