Getting a python virtual env error after installing Lion

白昼怎懂夜的黑 提交于 2019-12-17 15:30:00

问题


I haven't touched python and virtualenv in a while, and I believe I setup my MBP with virtualenv and pip, but have totally forgotten how this stuff works.

After installing lion, I'm getting this error when I open up a new terminal window:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is set properly.

Any tips on how to fix this?

Trying:

easy_install eventlet

I got this:

Traceback (most recent call last):
  File "/usr/local/bin/easy_install", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 2607, in <module>
    parse_requirements(__requires__), Environment()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 565, in resolve
    raise DistributionNotFound(req)  # XXX put more info here
pkg_resources.DistributionNotFound: setuptools==0.6c11

回答1:


I am a complete Python/Virtualenv novice. However, I had the exact same problem and found a solution that worked for me. I believe that this will vary greatly depending upon the way you originally setup Python & Virtualenv.

In my case, the Lion upgrade completely wiped out all contents of my /Library/Python/2.*/site-packages, but left the now broken executables (which link to the contents of this folder) in /usr/local/bin. I believe that this is the root cause of the cryptic "No module" import errors.

Reviewing my .bash_history, I had originally used easy_install to install pip, and then pip to install virtualenv and virtualenvwrapper. Once I repeated these steps, then I was able to re-enter my old virtual environments which still contained all the packages I had installed on 10.6. Note, however, that 10.6 shipped with Python 2.6 as default. If your packages require Python 2.6, you should change your default Python version to 2.6 first.

Step-by-step:

  1. I removed old Virtualenv configuration commands from my shell startup scripts (eg., .bash_profile). Start a new terminal session.
  2. (optional) Choose the version of Python you wish to use, eg.,
    defaults write com.apple.versioner.python Version 2.6
  3. sudo easy_install pip. It seems as though /Library/Python/2.*/site-packages now requires administrator privileges. I don't recall that being the case in 10.6 (or at least my bash history doesn't reflect that).
  4. sudo pip install virtualenv
  5. sudo pip install virtualenvwrapper
  6. Finally, I re-enabled those virtualenv configuration commands I disabled in step 1. A new terminal session had everything back the way it was (look at pip freeze -l to see local packages in this virtual environment). I think.



回答2:


My solution on Ubuntu 14.04 where I had installed python3.4 was to add this to the ~/.bashrc file so that the tail of it looked liked this:

#Setup virtual envwrapper for python in case default doesn't work
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.4
export VIRTUALENVWRAPPER_PYTHON

# needed for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/projects
source /usr/local/bin/virtualenvwrapper.sh

When I did pip freeze on regular python which was 2.7 it showed packages installed correctly but still complained. Even though it complained the virtualenv did work.

I have later removed the errors by making sure that all the files in the ~/.virtualenvs belong to the user and group rather than a smattering of them to root. Like this:

bash cd ~ sudo chown -R ubuntu:ubuntu .virtualenvs




回答3:


Have you installed virtualenv for the Python that you are using? If you are using the default Apple-supplied system Python 2.7, it doesn't come with the virtualenv package pre-installed. Use /usr/bin/easy_install to install it.

UPDATE: Just to be clear, on Lion 10.7, there is now an Apple-supplied Python 2.7 which is the default version when you use /usr/bin/python. Apple continues to also ship a Python 2.6 and a 2.5 in Lion as it did in 10.6 Snow Leopard. You can pick which version you want by explicitly referencing it, as in /usr/bin/python2.7, /usr/bin/python2.6, etc, and you can change the default that /usr/bin/python uses; read the Apple man page (man python) for details. The key point, though, is that, in general, Python packages are installed to a specific version. If you installed something for 2.6, you'll need to install it with python2.7 (or easy_install-2.7) if you want to use it with 2.7.




回答4:


Blankman,

I encountered the same error but my problem in hindsight was straightforward, i am sharing my work around below. YMMV.

I used (home)brew to install python 2.7.2 and am using it as my default. It is referenced via /usr/local/bin/python. The stock python install that came with Mac OS X Lion is referenced at /usr/bin/python.

--

The virtualwrapper.sh startup script checked the environment variable VIRTUALENVWRAPPER_PYTHON and if empty and populated it with '/usr/local/bin/python'. This was the reason for my error. Initializing it to '/usr/local/bin/python' fixed the problem for me.

HTH.

To clarify. I installed python 2.7.2 using brew and combine it with virtualenv to better manage my development environments. I try to steer clear of the preinstalled set up as much as I can.




回答5:


To fix the second problem you should reinstall setuptools.

And to fix the first one, just install virtualenvwrapper with that.

Basically, when you install Lion, you'll have to reinstall all your python site-packages.




回答6:


I had to reinstall setup-tools (in this case, the Python 2.7 egg for Mountain Lion), and then I was able to reinstall pip, and then virtualenv.




回答7:


For anyone else who is pulling out there hair... I was getting this same error but only when trying scp (secure copy) to one of my other macs. It took me a while to realize that the message was actually relevant to the Mac I was logging in. For os x .bash_profile runs at login and I wasn't correctly sourcing my .bashrc from the .bash_profile to pick up the environment settings Ned outlines. So make sure these setting are set for the machine you log into as well.




回答8:


yes, after upgrading to marvericks, need to install pip and virtualenv again by using /usr/bin/easy_install.




回答9:


I received this after upgrading to yosemite. To fix I had to run:

sudo pip install --upgrade setuptools


来源:https://stackoverflow.com/questions/6968914/getting-a-python-virtual-env-error-after-installing-lion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!