Python Virtualenv - No module named virtualenvwrapper.hook_loader
Asked Answered
B

16

82

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.

Bree answered 16/7, 2012 at 15:11 Comment(5)
Did you see this or you could try other way easy_installCassaundra
Thanks I looked at the post. But I cannot manage to install virtualenv for python 2.7. The message is that virtualenv is already installed for 2.6. I have run the following commands: ~ TK$ which python /Library/Frameworks/Python.framework/Versions/2.7/bin/python ~ TK$ sudo pip install virtualenv Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.6/site-packages/virtualenv-1.6-py2.6.egg Cleaning up...Bree
I just looked a virtualenv's status and it doesn't look like it supports Py 2.7.Dingle
Just a note - I hit this issue in a different case. I started making a virtualenv via mkvirtualenv and then stopped it (with Ctrl+C). Upon trying to make the virtualenv again, there were some leftovers from last time. I went into my envs folder and deleted the incompletely-built environment. After that, the same mkvirtualenv command worked properly.Archibaldo
Yibo Yang's comment was spot on for my Ubuntu installation as well. Just be careful with the spelling ... the above should be export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 with the "v" in VIRTUALENVWRAPPERTenet
B
54

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
Bree answered 16/7, 2012 at 21:6 Comment(8)
Thanks - after upgrading my Python 2.7 install on Mac Mountain Lion I had the same problem and this fixed it.Frederic
On Mountain Lion, the trick for me was ensuring the PATH for /opt/local/bin was set BEFORE the source ... line. I did not have to mess with the OSX Python links in /usr/bin or anything else. Also WORKON_HOME is now defaulted if not set to $HOME/.virtualenvsClos
+1 for @Clos comment. Also, for brew users: the actual path is usr/local/Cellar/python/<PYTHON-VERSION>/bin/.Canorous
Thanks, it helps me after compiling myself python 2.7.4 in Debian.Manouch
In my case the problem happened only why I was trying to remotely log into another machine. Hint, this means the problem is not on the local machine, but on the machine you're logging into! Thought I'd mention it in case someone is in that spot.Macri
Thanks! I had to use ez_setup to install setuptools bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py. After you download it, run sudo python ez_setup.py.Meetinghouse
Thanks, worked for me in Ubuntu 12.04. I had two Python versions installed, one in /usr/bin and another in /usr/local/bin, and pip and easy_install were using /usr/local/bin/python2.7.Spaceless
Reading this helped me, I just set VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/Current/bin/python3 in ~/.zshrcStigma
M
23

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`
Meetinghouse answered 5/9, 2013 at 5:0 Comment(1)
I'm installing python3 alongside my os python2 and setting VIRTUALENVWRAPPER_PYTHON solved my problem.Zimbabwe
M
16

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`  
Metabolism answered 7/1, 2019 at 1:10 Comment(0)
C
9

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
Cephalopod answered 21/4, 2017 at 18:3 Comment(3)
export VIRTUALENVWRAPPER_PYTHON=/usr/local/Cellar/python/3.6.5/bin/python3.6 for latest version.Beverage
in my bash_profile, export VIRTUALENVWRAPPER_PYTHON="which python3"Buttaro
@FaiZalDong I had to slightly modify your command in my Deb 11 profile to use backticks instead of quotes: export VIRTUALENVWRAPPER_PYTHON=`which python3`Sacramentalism
D
4

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.

Darciedarcy answered 21/4, 2015 at 3:58 Comment(0)
S
3

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
Square answered 27/1, 2016 at 16:30 Comment(0)
R
2

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
Riane answered 13/8, 2017 at 22:14 Comment(0)
E
1

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

Enzymology answered 27/12, 2016 at 8:7 Comment(0)
S
0

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.

Sepia answered 23/1, 2016 at 20:54 Comment(0)
S
0

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
Sleeping answered 6/7, 2016 at 19:43 Comment(0)
D
0

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

Doyle answered 18/1, 2017 at 5:33 Comment(0)
S
0

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.

Squall answered 23/8, 2017 at 14:55 Comment(0)
K
0

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.

Kahle answered 22/1, 2018 at 15:45 Comment(0)
P
0

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.

Phung answered 28/5, 2018 at 9:32 Comment(0)
M
0

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

brew install python2 --upgrade
Mikamikado answered 15/9, 2018 at 18:15 Comment(0)
B
0

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.

Blacken answered 30/5, 2019 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.