Python Virtualenv – No module named virtualenvwrapper.hook_loader

Question:

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.

Asked By: Thomas Kremmel

||

Answers:

The issue was solved following the steps below:

#switch the /usr/bin/python link to point to current python link
cd /usr/bin
sudo mv python python.bak
sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python python

Re-arrange the export command in order that it is placed before the virtualenv commands in my .bash_profile file:

PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH
export PATH

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

Re-Install setuptools, easy install and PIP. This is obviously needed in order that they work properly with the new python version:

sudo sh setuptools-0.6c11-py2.7.egg

sudo easy_install-2.7 pip

pip install virtualenv
Answered By: Thomas Kremmel

Also, if you have macports, make sure /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin is listed before /Library/Frameworks/Python.framework/Versions/2.7/bin and /usr/local/bin in PATH. Then set the following in you .profile:

export VIRTUALENVWRAPPER_PYTHON=`which python`
export VIRTUALENVWRAPPER_VIRTUALENV=`which virtualenv`
source `which virtualenvwrapper.sh`
Answered By: reubano

This happened to me and I solved it by re-installing pip. What had happend was that which pip gave /usr/bin/pip as a result, while which python gave /usr/local/bin/python. The path for pip should be /usr/local/bin/pip. This probably broke when I updated my Python installation.

If you follow the pip documentation you can easily reinstall pip for your current working Python setup. You need to:

  1. Download the get-pip.py script (directly linked from pip‘s documentation).
  2. Run python get-pip.py.

This solved the problem for me.

Answered By: Omar Trejo

I just installed python 3.5,tried the virtualenvwrapper and then had this problem. I came to realize that python3.5 was installed in /usr/local/bin/python3.5 and NOT /usr/bin/python3.5. So, I revised my .bash_profile script to look like the following and everything seems to work now

# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.5
export WORKON_HOME=$HOME/.virtualenvs
source /Users/bentaub/.virtualenvs/djangodev/bin/virtualenvwrapper.sh

I’m enough of a novice to not be sure how that ‘local’ in the path to python3.5 is going to affect me in the long run but, for now, it works.

Answered By: Ben

There are a number of things that can cause this error. If your environment is

  • CentOS 7, with python3 installed from epel-release
  • pip3 installed with python3.4 get-pip.py
  • virtualenvwrapper installed with pip3
  • A python virtual environment made with mkvirtualenv -p /usr/bin/python3.4

Then, for whatever reason, the virtual environment is created without the virtualenvwrapper library. You can solve it by simply installing it again, but this time from within the virtlualenv

[user@localhost ~] $ mkvirtualenv -p /usr/bin/python3.4 venv
Using base prefix '/usr'
New python executable in /home/user/.virtualenvs/venv/bin/python3.4
Also creating executable in /home/user/.virtualenvs/venv/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/get_env_details
/home/user/.virtualenvs/venv/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')
/home/user/.virtualenvs/venv/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')

# the virtualenv should now activated
(venv)[user@localhost ~] $ pip install virtualenvwrapper
Answered By: drs

I had this problem after uninstalling the virtualenvwrapper package. When I logged in to any user (or su to a different one), I would get:

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.

The solution was to delete the /etc/bash_completion.d/virtualenvwrapper file.

Edit:

Do not delete the above file or it won’t be recreated if you reinstall virtualenvwrapper. Instead what you need to do is purge the virtualenvwrapper package when you uninstall it. Like this on Debian:

apt-get remove --purge virtualenvwrapper
Answered By: Mike

I get the same error . Found out I had old version of pip . I fixed the error by simply upgrading the pip .

Answered By: Chinmay Das

Try to uninstall your virtualenv and virtualenvwrapper and install it again using pip in version 2.7 (I think).

I encountered the same error and I just did this and solved my problem.

I using U

Answered By: manilaT

In my case, adding this line into my .zshrc file did the trick,

export VIRTUALENVWRAPPER_PYTHON=/usr/local/Cellar/python/2.7.13/bin/python2.7
Answered By: pecai

I just had to make sure that /usr/local/bin/python existed.

For me it was a simple:

ln -s /usr/local/bin/python2.7 /usr/local/bin/python
Answered By: dustinfarris

Even though there is an accepted answer I thought I would put what fixed it for me.

Firstly I installed Python and had just upgraded it via Homebrew. I am also using ZSH so if some bits don’t quite match your output then that might be why.

By running brew info python and looking through the output I found the following nice bit of information:

If you wish to have this formula's python executable in your PATH then add
the following to ~/.zshrc:
    export PATH="/usr/local/opt/python/libexec/bin:$PATH"

So I added this to my terminal startup script as shown and the error n longer displays.

Note: I inserted this into another part of my PATH and the error persisted on start up.

Answered By: Skepi

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.

Answered By: ArdentLearner

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.

Answered By: user3837712

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

brew install python2 --upgrade
Answered By: Jay

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`  
Answered By: Kalob Taulien

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.

Answered By: MeNinLes