Python Virtualenv - No module named virtualenvwrapper.hook_loader

Viewed 99135

I'm running Mac OS 10.6.8. and wanted to install in addition to python 2.6 also python 2.7 and use python 2.7 in a new virtualenv. I executed the following steps:

I downloaded python 2.7 and installed it:

http://www.python.org/ftp/python/2.7.3/python-2.7.3-macosx10.6.dmg

Then I run the command to setup a new virtualenv using python2.7:

mkvirtualenv --python=python2.7 mynewenv

My .bash_profile looks like the following:

# needed for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh


# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

Now when I open the console I get the following error message.

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/local/bin/python and that PATH is set properly.

I also found in a different post that I should upgrade virtualenvwrapper. That did not help.

sudo pip install virtualenvwrapper --upgrade

Any help would be appreciated.

16 Answers

For anyone using Ubuntu 18.04 and Python 3+, this did the trick for me:

which python3 # outputs /usr/bin/python3 
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3  
source `which virtualenvwrapper.sh`  

Ran into a similar issue after installing Conda/Anaconda project. This question was quite helpful in resolving my issue on MAC.Resolving the issue had my .zshrc relevant portion looking like this:

export VIRTUALENVWRAPPER_PYTHON=$HOME/Applications/conda/bin/python
source $HOME/Applications/conda/bin/virtualenvwrapper.sh

This is depended on where I have conda installed and you'll have to figure that in your own case. To get the specifics for your given environment depending on where you've installed anaconda you can use the following:

$  ~/ -name virtualenvwrapper.sh # to see where you have this. May already be prefilled in your shell profile[.zshrc or .profile]

$ which python   # to know the default python your project or rather where conda has installed python for you

DON'T FORGET TO UNINSTALL AND INSTALL virtualenv and virtualenvwrapper as highlighted in other answers.

Just bumped into this issue on a Centos 7.4.

None of the above answers suited my case. After quite a bit of digging around I pinpointed this to too-strict file permissions in python libs (I guess python installation on Centos differs a bit from other POSIX systems).

So, if everything else fails you might want to check that your python libs are readable by the user you're trying to run virtualenvwrapper with.

In particular check: /usr/lib/python3.6 /usr/lib64/python3.6 (amend the paths for different python versions).

If you see that group and others lack read and execute permissions in there then add them: sudo chmod og+rx -R /usr/lib/python3.6 sudo chmod og+rx -R /usr/lib64/python3.6

Note: I'm not sure whether this works against a Centos security policy but it's probably safe as long as you don't give write persmisions.

In my situation (OS X 10.13.6), this did it

brew install python2 --upgrade

I had the same problem as this one and spent so much time on configuring out what was wrong. And I finally found out what was wrong.

First I looked for where virtualenvwrapper folder exists. In my case /usr/local/lib/python3.7/site-packages. Inside the folder is hook_loader.py that caused the error.

Next, I used python script.

python3

import sys;print('\n'.join(sys.path))

I couldn't find the /usr/local/lib/python3.7/site-packages directory so, at last I wrote,

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages

to .bashrc file. Done.

Meaning of PYTHON PATH

As you can see in above link, PYTHONPATH augment the default search path for modules.

Related