Python Virtualenv - No module named virtualenvwrap

2019-01-12 18:18发布

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.

15条回答
Summer. ? 凉城
2楼-- · 2019-01-12 18:50

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
查看更多
甜甜的少女心
3楼-- · 2019-01-12 18:51

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
查看更多
女痞
4楼-- · 2019-01-12 18:51

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

brew install python2 --upgrade
查看更多
趁早两清
5楼-- · 2019-01-12 18:56

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`  
查看更多
你好瞎i
6楼-- · 2019-01-12 18:57

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.

查看更多
Luminary・发光体
7楼-- · 2019-01-12 18:57

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.

查看更多
登录 后发表回答