53

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 9
    Have you installed `virtualenvwrapper` successfully **for that version of Python**? If you run `/usr/bin/python3.4` in the terminal and then try to `import virtualenvwrapper`, what happens? – jonrsharpe Mar 19 '15 at 16:23
  • ImportError: No module named 'virtualenvwrapper' - I guess not. I installed it for the base interpreter with is python 2.7 here – user1592380 Mar 19 '15 at 16:39
  • 8
    You probably need to `pip3 install virtualenvwrapper` to get it installed for the 3.x interpreter. Alternatively, note that `virtualenvwrapper` installed under 2.x *can* still create 3.x `virtualenv`s, using the `-p` parameter (see e.g. http://stackoverflow.com/q/1534210/3001761). – jonrsharpe Mar 19 '15 at 16:43
  • Thank you, you got it working! Would you mind putting in an answer? I've gone with your second suggestion. Please see the edit. – user1592380 Mar 19 '15 at 16:59
  • You shouldn't edit the outcome into the question - I'm happy for you to take what you've learned and write your own answer, instead! – jonrsharpe Mar 19 '15 at 17:00
  • @jonrsharpe Thank you very much for that comment to hit that one needs to check if the python interpreter that has virtualenvwrapper installed is also the one set in `VIRTUALENVWRAPPER_PYTHON`. – Philipp Doerner Jun 08 '20 at 07:57

10 Answers10

43

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

tropicalfish
  • 1,230
  • 1
  • 16
  • 29
  • 7
    This issue seems prevalent on Macs, but if you're on Ubuntu, you'll want to use '/usr/bin/python3', 'usr/local/bin/virtualenv', and '/usr/local/bin/virtualenvwrapper.sh', assuming you've installed to the default locations. – codepringle Jun 03 '16 at 19:10
39

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
mc_kaiser
  • 717
  • 8
  • 18
nphadke
  • 502
  • 5
  • 8
16

On Ubuntu 20.04, the problem can occur when trying to install virtualenvwrapper with python 3.8 (python 3 default) and init the wrapper with python 2.7 (python 2 default).

TL;DR

Manually set python3 interpreter

export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source /usr/local/bin/virtualenvwrapper.sh

In more details, Why does this happen?

Let's get some informations:

$ which python
/usr/bin/python

$ python --version
Python 2.7.18rc1

$ which python3
/usr/bin/python3

$ python3 --version
Python 3.8.2

$ pip3 --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Following the guidelines, we are asked to install virtualenvwrapper with pip (python 3): pip3 install virtualenvwrapper

Current stable version (4.8.4) of virtualenvwrapper is linking to default python version, which we saw it's python 2.7:

VIRTUALENVWRAPPER_PYTHON="$(command \which python)"

So, the problem is that we installed virtualenvwrapper in python3 and try to init with python2 (sourcing shell script). The fix is therefore to init with python 3 by overriding default.

But, it is very likely that one of the next releases will include a fix already merged onto master that look from highest to lowest python version:

if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
    for NAME in python3 python2 python
    do
        python_executable="$(which $NAME 2>/dev/null)"
        if ! [ -z "$python_executable" ]
        then
            if $python_executable -m 'virtualenvwrapper.hook_loader' --help >/dev/null 2>&1
            then
                VIRTUALENVWRAPPER_PYTHON=$python_executable
                break
            fi
        fi
    done

Using in the documentation, the fix is to manually set the Python interpreter to use before sourcing: Not python (2.7) but python3 (3.8 here)

export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source /usr/local/bin/virtualenvwrapper.sh
Zoe
  • 27,060
  • 21
  • 118
  • 148
x0s
  • 1,648
  • 17
  • 17
8

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).

enderland
  • 13,825
  • 17
  • 98
  • 152
8

Since I change python versions every now and then, this configuration has been working so far, since it's dynamic:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/development
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
export VIRTUALENVWRAPPER_VIRTUALENV=$(which virtualenv)
source $(which virtualenvwrapper.sh)

And then followed by:

source ~/.zshrc
pmadruga
  • 99
  • 1
  • 3
2

Make sure that you are using the correct version of python. In my case I was using

\usr\bin\python3

Instead of

\usr\local\bin\python3.7
Zoe
  • 27,060
  • 21
  • 118
  • 148
briankip
  • 2,502
  • 2
  • 23
  • 26
1

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 2
    Please ensure that this is a useful, standalone answer - i.e. explaining *why* this works (and including, presumably, the changes you've made to `.bashrc`). – jonrsharpe Mar 19 '15 at 17:06
1

For those facing the same issues while using Ubuntu 18.04, please note that .bashrc needs the following edits.

Change source /usr/local/bin/virtualenvwrapper.sh to

source ~/.local/bin/virtualenvwrapper.sh

NOTE that local directory is hidden

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rushiraj Parmar
  • 113
  • 2
  • 3
  • 8
1

No solution above helped me.

Here is my way which was working for me:

# if doesn't work try sudo pip uninstall virtualenvwrapper
pip uninstall virtualenvwrapper
# rm old virtualenv scripts
rm ~/.local/bin/virtualenv*
# re-install viertualenv
pip install --user  virtualenvwrapper
n.shalnov
  • 79
  • 1
  • 3
-1

My Unified OpenCV environment setup function that works seamlessly across MAC and Linux environments...

enter image description here

Sudhanshu Gupta
  • 57
  • 1
  • 2
  • 5