26

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
Kev
  • 118,037
  • 53
  • 300
  • 385
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

9 Answers9

41

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.
mbauman
  • 30,958
  • 4
  • 88
  • 123
22

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

hum3
  • 1,563
  • 1
  • 14
  • 21
  • This is a really cool solution. I would like to add that if you run: 'virtualenv -h' you get the default interpreter that virtualenv was installed with and you can just use that for VIRTUALENVWRAPPER_PYTHON – edu222 Nov 04 '16 at 16:15
  • my problem was regarding the version of python. `virtualenvwrapper` was not installed in my `/usr/bin/python` but `/usr/bin/python3.5`. – Fahad Siddiqui May 25 '17 at 06:49
6

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.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • I had it working, but after installing lion I get this message whenever I open a new terminal window. – Blankman Aug 06 '11 at 22:30
  • When you installed Lion over an existing Snow Leopard system, you will "lose" all the 3rd-party packages that you installed in the Apple-supplied system Python because the default on 10.7 is now Python 2.7. If you changed the definition to be VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.6, chances are it would still work because there is still a system Python 2.6 on 10.7. But if you want to use the system Python 2.7 as your default, you need to install a copy of `virtualenv` for it. Just do `sudo /usr/bin/easy_install virtualenv` – Ned Deily Aug 06 '11 at 23:31
  • @Blankman: Did that answer your question? If so, please mark the answer as accepted. If not, please refine the question so that someone can provide an acceptable answer. – Ned Deily Aug 08 '11 at 03:48
  • @Ned Deily I've tried your advice to re-easy_install with no change in the error message. I've checked .profile and .bashrc, both of which are empty. – Jon Hadley Aug 18 '11 at 21:08
  • @Jon Hadley: What exactly did you reinstall where? You need to have used the appropriate `easy_install` to install `virtualenv` and `virtualenvwrapper` for the Python instance you are using. Neither of those packages modify `.profile` or `.bashrc` (nor `.bash_profile`). It's kind of difficult to diagnose things like this without more information. Perhaps you should pose your own question. – Ned Deily Aug 18 '11 at 21:28
5

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.

Amar
  • 231
  • 3
  • 3
  • This helped me to figure out the occurrence of this error in my case. My `VIRTUALENVWRAPPER_PYTHON` env variable was set to the version of Python inside my virtualenv. I believe this happened due to a weird `postactivate` hook that I set, but I didn't remember that the env variable needed to be set to the system's version of python until I saw your post. – Brian Peterson Aug 07 '14 at 01:50
2

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.

Pavel Shvedov
  • 1,284
  • 11
  • 8
1

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.

Ima
  • 680
  • 1
  • 15
  • 28
1

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.

SmileBot
  • 19,393
  • 7
  • 65
  • 62
1

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

allen.bang
  • 99
  • 1
  • 6
  • This worked for me, I simply used `pip uninstall virtualenv` and removed pip directly from usr/local/bin. I then reinstalled botH using `sudo easy_install` OSX 10.9.2 I can now create virtual enviroments – Deepend Mar 03 '14 at 19:32
0

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

sudo pip install --upgrade setuptools
donturner
  • 17,867
  • 8
  • 59
  • 81