9

When trying to install virtualenv using a brewed python, I get the following error:

$ pip install virtualenv
Requirement already satisfied (use --upgrade to upgrade): \
  virtualenv in /Library/Python/2.7/site-packages/virtualenv-1.9.1-py2.7.egg
Cleaning up...

So clearly pip is somehow looking into the system python folder instead of the brewed one in /usr/local. How can I avoid this?

The brew doctor finds no problems, and the path priority is set ok.

I tried to remove the virtualenv package from the system python, so I could install virtualenv with the brewed pip. But then I could not create virtualenvs, as I got this error:

$ virtualenv datatransfer/
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2603, in <module>
    working_set.require(__requires__)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 666, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 565, in resolve
    raise DistributionNotFound(req)  # XXX put more info here
pkg_resources.DistributionNotFound: virtualenv==1.9.1

So I have not managed to avoid the system virtualenv.

What I am doing wrong?

RogerFC
  • 329
  • 3
  • 15
  • Take a look at this wiki page: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python. It explains how to edit your PYTHONPATH and PATH so that your system finds the brewed python first. – SethMMorton May 31 '13 at 15:53
  • @SethMMorton The PATH is already correct, and the PYTHONPATH is empty. What I see in this wiki is that the brewed python also looks for modules at /Library/Python/2.7/site-packages. But then will the virtualenvs created with the system command in that folder use the brewed python? – RogerFC May 31 '13 at 16:41
  • what happens when you type `which python` and `which pip`? – SethMMorton May 31 '13 at 16:51
  • @SethMMorton the brewed python and pip are found in /usr/local/bin – RogerFC May 31 '13 at 18:16
  • Unfortunately I have no means of reproducing the problem now, so I cannot mark any of the answers as correct. – RogerFC Jul 20 '14 at 12:02
  • The URL shared by @SethMMorton has been replaced by https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Homebrew-and-Python.md. – Bouke Aug 16 '15 at 06:14

5 Answers5

19

I had the same problem and I managed to solve it by uninstalling any brew versions of Python and virtualenv

brew uninstall python
brew uninstall pyenv-virtualenv

Manually moving all the virtualenv* files I found under /usr/local/bin to another folder

sudo mkdir /usr/local/bin/venv-old
sudo mv /usr/local/bin/virtualenv* /usr/local/bin/venv-old/

Opening a new terminal tab and double-checking that I'm in a clean state

which python # => /usr/bin/python
which virtualenv # => virtualenv not found

And installing Python and virtualenv(wrapper) cleanly using the recommended method (here and here):

brew install python --with-brewed-openssl
# Open a new terminal tab now (to access /usr/local/bin/python)
pip install virtualenv
pip install virtualenvwrapper
metakermit
  • 21,267
  • 15
  • 86
  • 95
  • This is the most solid solution I found without needing to `sudo` re-instal anything. Thank you! – Geekfish Mar 03 '14 at 10:41
  • If, like on my system, `/usr/local/bin/virtualenv` continues to exist even after running `pip uninstall virtualenv`, it seems you can manually delete `/usr/local/bin/virtualenv` and then `pip install` provides a version of virtualenv that runs successfully. – npdoty Jul 16 '14 at 00:20
  • Yes, that is why I suggested moving all `/usr/local/bin/virtualenv*` stuff to a new folder. I don't like permanently deleting anything outside of my home folder, as you never know what system utility might depend on it. – metakermit Jul 16 '14 at 09:34
4

I had the same issue,

for me

sudo mv /usr/local/bin/virtualenv* /usr/local/bin/venv-old/

the

pip install -I virtualenv

I didn't have to uninstall and reinstall python.

1

What really caused and issue for me was the presence of .pydistutils.cfg in my $HOME directory. That's what forced me to have to use: sudo pip install <package>.

If you ever have to use sudo to install a package via pip, you're doing it wrong and you need to start troubleshooting your system right away; else everything is going to fail and you won't know why.

Running: brew doctor will warn you about the .pydistutils.cfg file, so don't forget to monitor the status of that command after making core changes to your system.

It took me quite a while to track this down, but all is well now. This really helped

A-Dubb
  • 1,670
  • 2
  • 17
  • 22
1

I had the same problem and found the solution on a Google group. It seems that /usr/local/bin/virtualenv was not updated so if you open it, you will see a reference to the old virtualenv 1.9.1 when a newer version has been installed on your system.

So you just need to open /usr/local/bin/virtualenv and replace it with:

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from virtualenv import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

And that worked for me!

Source: https://groups.google.com/forum/#!topic/python-virtualenv/hL_QDPJsnuY

Antoine Brunel
  • 1,065
  • 2
  • 14
  • 30
  • this worked for me! indeed I had a very old virtualenv script right there, which for some reason defied getting uninstalled. – gabe May 01 '15 at 16:02
0

I found that unlike you my paths were wrong according to brew doctor. But after correcting that I still had the issue (no surprises given your description) so I uninstalled virtualenv then reinstalled but this time with sudo, sudo pip install virtualenv

This resolved the issue, which indicates that I've either got another problem with the system configuration in general or someone who has more then my one hour of Python experience will be able to point out why it's not good (I think it's related to global installing being considered a bad practice).

Seth is onto something with which seems you see the same as me,

~ which python results in /usr/local/bin/python

~ ls -l /usr/local/bin/python gives me _permissions_etc_ /usr/local/bin/python -> ../Cellar/python/2.7.5/bin/python

Pip

which pip pointed to /usr/local/bin/pip

ls -l /usr/local/bin/pip shows _permissions_etc_ /usr/local/bin/pip -> ../Cellar/python/2.7.5/bin/pip

Virtualenv (I don't have an output prior to the sudo pip install)

which virtualenv is /usr/local/bin/virtualenv

ls -l /usr/local/bin/virtualenv shows _permissions_etc_ /usr/local/bin/virtualenv

When I created and use a virtualenv I get the following,

workon ve_test
which python
/Users/_username_/.virtualenvs/ve_test/bin/python
(ve_test)➜  ~PIP_VIRTUALENV_BASE  python --version
Python 2.7.5

FWIW my .zshrc has the following,

# Python related
# http://hackercodex.com/guide/python-virtualenv-on-mac-osx-mountain-lion-10.8/
# http://docs.python-guide.org/en/latest/dev/virtualenvs.html
source /usr/local/bin/virtualenvwrapper.sh
# virtualenv should use Distribute instead of legacy setuptools
export VIRTUALENV_DISTRIBUTE=true
# Centralized location for new virtual environments
export PIP_VIRTUALENV_BASE=$HOME/Projects/PythonVirtualEnv
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache
Shane Maiolo
  • 171
  • 4