Can't pip install virtualenv in OS X 10.8 with

2019-01-17 17:36发布

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?

5条回答
成全新的幸福
2楼-- · 2019-01-17 18:13

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.

查看更多
混吃等死
3楼-- · 2019-01-17 18:20

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
查看更多
淡お忘
4楼-- · 2019-01-17 18:25

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

查看更多
Lonely孤独者°
5楼-- · 2019-01-17 18:33

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

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-17 18:34

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
查看更多
登录 后发表回答