33

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.

Community
  • 1
  • 1
mikeonly
  • 465
  • 1
  • 4
  • 8
  • Did you use `sudo` when installing `virtualenv`? Maybe it got installed as a user-private package somehow and that's causing the troubles. (Like not having wherever binaries are placed during that installation on `PATH` etc.) – millimoose Dec 29 '15 at 02:54
  • @millimoose I tried both without `sudo` and with it, the result is the same. It is unlikely the problem of privileges, as it is possible to use `virtualenv.py` [locally from source](https://virtualenv.pypa.io/en/latest/installation.html). Using `virtualenv.py` from tarball also strangely fails to ship `pip`. – mikeonly Dec 29 '15 at 02:59

5 Answers5

24

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.

Community
  • 1
  • 1
emunsing
  • 9,536
  • 3
  • 23
  • 29
  • Thanks for the thorough answer! I just wanted to say that cannot confirm the answer, as I do not longer experience the mentioned issue. I have a new computer and, thankfully, cannot reproduce that. However, if someone can confirm that this solution works, I will happily approve your answer. – mikeonly Dec 14 '16 at 11:42
  • This is absolutely correct. I removed the file ~/.pydistutils.cfg and all my problems were solved. Thank you for summarizing a lot of information in one answer. – carlosdc Jan 15 '17 at 06:48
  • 1
    I cannot find the file `.pydistutils.cfg`, used `locate .pydistutils.cfg` but couldn't find it, same error and debugging shows the same problems. Any other solution. System: Ubuntu – Vaulstein Apr 10 '17 at 10:24
  • 1
    @Vaulstein : This question and answer were specific to Mac OS X, probably because .pydistutils.cfg is edited by homebrew (for Mac). I'm not able to replicate the problem on the Ubuntu systems I have access to, but if you find a solution definitely post a comment, or if you don't find a solution then maybe post a Linux-specific question and link to this! – emunsing Apr 11 '17 at 18:49
  • @emunsing, it seems like i am getting same problem of not able to install with pip inside venv. But I am not getting your #4 step. When I do 'which pip' it gives me my environment's path. Also I am not getting any .pydistutils.cfg file in my home directory. Is there any other cause generating this problem on my machine? Please help. – Mr Singh Jun 22 '17 at 18:33
  • @MrSingh It sounds like you have a different question- and to diagnose it, you'll need to provide details on what happens when you attempt to `pip install` a package. As this stackoverflow question is specific to 'virtualenv does not include pip' you may want to create a new question if you're not able to find a solution, so that others with the same problem can also be helped. – emunsing Jun 23 '17 at 19:10
  • Hi @emunsing, I actually got the solution by hitting my head and doing hit and trial. So what happens is that if I create virtual environment folder 'my-python-env' in my home directory directly then it's fine otherwise if I go one level deeper by creating a folder first (ex: 'envs') in home directory and then create virtual environment folder 'my-python-env' inside it, pip does not work. Anyways thanks. :) – Mr Singh Jun 29 '17 at 18:27
  • `which -a pip` would output what you are expecting – dǝɥɔS ʇoıןןƎ Jul 02 '19 at 08:12
4
  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.

cel
  • 30,017
  • 18
  • 97
  • 117
  • Double-checked everything you said. Everything points to `/usr/local/bin/…` first, as well as `virtualenv`, which is at version 13.1.2. `env` shows nothing related to `PYTHON`, although it indeed looks like environment variables problem. Anywhere else to look? – mikeonly Dec 29 '15 at 08:33
  • did you remove the symlink you created before? Can you reinstall `virtualenv` and add the output you get from `pip`, also the output from `virtualenv` when you create a new venv. Also can you print the commands you use to activate the `venv` and `echo $PATH` after activating the venv. – cel Dec 29 '15 at 08:41
  • Symlink is created properly only when I use `--isolated` option, it makes `pip` to correctly generate `virtualenv` in `/usr/local/bin`. Using this `virtualenv` still does not give `pip` in created `venv`. Here is the output: http://pastebin.com/BbG8Ytap – mikeonly Dec 29 '15 at 09:03
  • I have a similar problem; virtualenv is not installing to /usr/local/bin but instead to /Users/username/bin (OS X El Capitan, python 2.7, pip 7.1.2). The output of `env |grep PYTHON` is empty... any ideas; @cel? – emunsing Aug 23 '16 at 03:58
  • @emunsing, what is the output of `which -a pip`? – cel Aug 23 '16 at 05:20
  • @cel the output I get is `/usr/local/bin/pip \n /Users/emunsing/bin/pip` – emunsing Aug 23 '16 at 05:41
2

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

Nazar
  • 21
  • 1
0

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.

spinkus
  • 7,694
  • 4
  • 38
  • 62
0

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

MS MS
  • 3
  • 2