Python Virtualenv - No module named virtualenvwrapper.hook_loader
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.
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
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`
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`
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