No module named 'virtualenvwrapper'

2019-03-12 02:16发布

I am working to set up a Django project on Amazon EC2 with an Ubuntu 14.04 LTS instance. I want to write my code using Python 3. I've been advised that the best way to do this is to use virtualenvwrapper. I've installed virtualenvwrapper successfully and put

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.4
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

into my .bashrc file. Now I see:

 /usr/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportErro
 r'>: No module named 'virtualenvwrapper')
 virtualenvwrapper.sh: There was a problem running the initialization hooks.     

 If Python could not import the module virtualenvwrapper.hook_loader,
 check that virtualenvwrapper has been installed for
 VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.4 and that PATH is
 set properly.

How can I fix this?

4条回答
Evening l夕情丶
2楼-- · 2019-03-12 02:32

If you use brew to install python, you will want to ensure that you set this environment variable:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python

in your bash_profile (or whatever shell you are using).

查看更多
Viruses.
3楼-- · 2019-03-12 02:36

Instead of specifying a different python interpreter with -p flag, you can also config your desired interpreter as default.

According to virtualenvwrapper's documentation, virtualenvwrapper.sh finds the first python and virtualenv programs on the $PATH and remembers them to use later.

If your virtualenvwrapper is not installed on your OS's default python interpreter (/usr/bin/python), make sure you override the environment variables as below:

  • VIRTUALENVWRAPPER_PYTHON to the full path of your python interpreter
  • VIRTUALENVWRAPPER_VIRTUALENV to the full path of the virtualenv

For example, on my .bash_profile (Mac):

#virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.5/bin/virtualenv
source /Library/Frameworks/Python.framework/Versions/3.5/bin/virtualenvwrapper.sh

Reload your new variables by running source ~/.bash_profile

查看更多
姐就是有狂的资本
4楼-- · 2019-03-12 02:43

I had the same problem after the recent Homebrew updates.

In the past, most people would have run pip install virtualenvwrapper into the system site packages and it would have worked.

Homebrew broke this workflow by 1) no longer shadowing the system python, and 2) no longer symlinking pip to pip2/pip3.

Most users will realize this when they can't find pip, and then try to use pip2/pip3. However, using pip2/pip3 will create a problem because virtualenvwrapper is now installed for python2/python3, but not for python. So when virtualenvwrapper runs and calls python, it won't find the virtualenvwrapper/virtualenv python packages in the system python's site packages.

Explicitly setting VIRTUALENVWRAPPER_PYTHON is the cleanest fix, and not a hack. Here's how I did it in my dotfiles

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
查看更多
霸刀☆藐视天下
5楼-- · 2019-03-12 02:49

Following Jon's advice I ran:

 ubuntu@ip-172-31-22-65:~$ mkvirtualenv -p /usr/bin/python3.4 env1
 Running virtualenv with interpreter /usr/bin/python3.4
 Using base prefix '/usr'
 New python executable in env1/bin/python3.4
 Also creating executable in env1/bin/python
 Installing setuptools, pip...done.
 (env1)ubuntu@ip-172-31-22-65:~$ deactivate
 ubuntu@ip-172-31-22-65:~$ ls
 ubuntu@ip-172-31-22-65:~$ ls -a
 .  ..  .bash_history  .bash_logout  .bashrc  .cache  .pip  .profile  .ssh  .virtualenvs
 ubuntu@ip-172-31-22-65:~$ workon
 env1
 ubuntu@ip-172-31-22-65:~$ workon env1
 (env1)ubuntu@ip-172-31-22-65:~$ which python
 /home/ubuntu/.virtualenvs/env1/bin/python
 (env1)ubuntu@ip-172-31-22-65:~$  python -V
 Python 3.4.0

I've left the .bashrc as listed above. As Jon stated above installing virtualenvwrapper installs on the default python, and uses the default python in any virtualenv you create unless the -p flag is used to specify a different python interpreter.

Thank you Jon!

查看更多
登录 后发表回答