/usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')
Asked Answered
P

18

100

I'm trying to follow How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04 and I'm failing at the very earlier stage, due to amount of output, I placed all related information into Pastebin.com - #1 paste tool since 2002!.

the actual error:

root@alexus:~# echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
root@alexus:~# source ~/.bashrc
/usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: 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 and that PATH is
set properly.
root@alexus:~#

I'm following how to to the teeth, what am I doing wrong?

Paolo answered 19/10, 2015 at 14:11 Comment(5)
Did you try pip install virtualenvwrapper --upgrade ? If that won't work, please follow this SO thread: #11507686Duntson
@Alexander I just installed python-pip and virtualenv & virtualenvwrapper. Do I still need to run upgrade? I'm doing fresh install.Paolo
After you installed all of the above, just run sudo apt-get update, then virtualenv myproject. Please make sure that your pip version is compatible with your python versionDuntson
BTW, I've never installed virtualenvwrapper and I'm using uwsgi with nginx on a daily basis. I don't recommend installing it unless it's mandatory for you to use it.Duntson
@Alexander my system is up to date. I actually was able to get it to work by switching VIRTUALENVWRAPPER_PYTHON from python3 to python. I need both virtualenvwrapper AND uwsgi for my django application.Paolo
P
100

Thanks to @Alexander, I was able to fix my issue by changing the line in ~/.bashrc:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

to

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python

due to backwards compatibility.

Paolo answered 20/10, 2015 at 14:21 Comment(7)
This worked for me, but in my case I needed to change it to /usr/bin/python3.6. It needs to match whatever version you're using.Carabineer
Worked after using sudo pip install virtualenv virtualenvwrapper instead of sudo pip3 install virtualenv virtualenvwrapperCommunalism
This line needs to be changed in ~/.bashrc, right? Maybe add that for noobs like meAfrika
I ls /usr/bin/python and it links to "python2". But my system is ubuntu 20, I thought python 2 did not exist thereGormand
In my case I try use pyenv + virtualenvwrapper (not the standalone one, but the pyenv plugin). So I have changed python -> python3 (oposite as in your answer). Otherwise the plugin wasn't recognized. The other possibility was to do for python2: apt install python-pip, pip install virtualenvwrapper (ie. standard virtualenvwrapper, not the pyenv wrapper).Parik
@alexus, I installed virtualenv using pip3 and added respective export statements to ./bash_profile. Entris isn bash_profile: export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source /usr/local/bin/virtualenvwrapper.sh. It did not work. But then I installed same using pip and it worked. No changes to bash_profile. Will you please explain why did it work this time?Aldenalder
Man, you just saved me a ton of hassle. I'd give you 2 upvotes if I could.Portwine
F
106

I faced similar problem, when using python3 with virtualenvwrapper,

sudo apt-get install python3-pip

Then install virtualenv and virtualenvwrapper from pip3,

sudo pip3 install virtualenv virtualenvwrapper

then source it again,

source $HOME/.bashrc 
Fellers answered 11/10, 2016 at 17:38 Comment(3)
This method also worked for me on OSX Mojave. using sudo pip3 install virtualenv virtualenvwrapperErinnerinna
I need to use python3 for some LDAP functionality, so this is probably the best route to go for me. I initially used the solution proposed by @alexus, which worked, however I'm not sure deviating from a python3 specific setting is not going to cause issues down the road.Macao
After upgrading to Ubuntu 23.04, the sudo pip3 install... command displays error: externally-managed-environment, as installing packages globally with pip is now discouraged. The following command worked as a replacement: sudo apt update ; sudo apt install python3-virtualenv python3-virtualenvwrapperWilmoth
P
100

Thanks to @Alexander, I was able to fix my issue by changing the line in ~/.bashrc:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

to

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python

due to backwards compatibility.

Paolo answered 20/10, 2015 at 14:21 Comment(7)
This worked for me, but in my case I needed to change it to /usr/bin/python3.6. It needs to match whatever version you're using.Carabineer
Worked after using sudo pip install virtualenv virtualenvwrapper instead of sudo pip3 install virtualenv virtualenvwrapperCommunalism
This line needs to be changed in ~/.bashrc, right? Maybe add that for noobs like meAfrika
I ls /usr/bin/python and it links to "python2". But my system is ubuntu 20, I thought python 2 did not exist thereGormand
In my case I try use pyenv + virtualenvwrapper (not the standalone one, but the pyenv plugin). So I have changed python -> python3 (oposite as in your answer). Otherwise the plugin wasn't recognized. The other possibility was to do for python2: apt install python-pip, pip install virtualenvwrapper (ie. standard virtualenvwrapper, not the pyenv wrapper).Parik
@alexus, I installed virtualenv using pip3 and added respective export statements to ./bash_profile. Entris isn bash_profile: export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source /usr/local/bin/virtualenvwrapper.sh. It did not work. But then I installed same using pip and it worked. No changes to bash_profile. Will you please explain why did it work this time?Aldenalder
Man, you just saved me a ton of hassle. I'd give you 2 upvotes if I could.Portwine
D
37

I encountered a similar issue with virtualenvwrapper complaining not being to find virtualenvwrapper.hook_loader. I was able to narrow down to this particular line in my .bash_profile

source /usr/local/bin/virtualenvwrapper.sh

as seen below

$ source /usr/local/bin/virtualenvwrapper.sh
/usr/local/opt/python3/bin/python3.6: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: 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/local/bin/python3 and that PATH is
set properly.

After some trial and error, it turns out that a reinstallation of the virtualenvwrapper package resolved it. This was a manifest of the fact that I recently upgrade python3 (from version 3.5.2 to 3.6.1) via homebrew and in turn it broke virtualenvwrapper's shell script that hardcoded references to the older version of python3 (in my case it was 3.5.2). In short, this below line should fix it (at least in my case it did).

pip3 install virtualenvwrapper
Doubletree answered 18/4, 2017 at 20:37 Comment(4)
Also I noticed that homebrew or some other installed packages at some point in the past that blew away the symlink at /usr/local/bin/python3, so somehow re-brew python3 via homebrew to restore the /usr/local/bin/python3 also make it work again.Doubletree
This fixed it for me! Thanks!Coleridge
@Doubletree you can restore the symlink of /usr/local/bin/python3 with brew link [email protected]Convince
@Doubletree thanks man! of all the thread, this worked for me. I had /usr/bin/python and /usr/bin/python3 correctly setup !Mckenna
J
19

In macOS Sierra, If you installed virtualenv package using pip3, add following to .bash_profile

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3

or

export VIRTUALENVWRAPPER_PYTHON=$(which python3)

after that every new virtualenvironment you created using workon myvenv uses python3 as python interpreter

Jurel answered 6/8, 2017 at 17:19 Comment(3)
export VIRTUALENVWRAPPER_PYTHON=$(which python3) worked for meAntepast
export VIRTUALENVWRAPPER_PYTHON=$(which python3) worked for me as well. I think this happened because I have two versions of python (2.7 default and 3). With this command, it picks the right one based on virtualenv.Dita
Thanks! I spent hours trying to find out what was wrong. In my case I am using pyenv so just had to use export VIRTUALENVWRAPPER_PYTHON=$(pyenv which python)Armlet
N
16

I had already installed virtualenv so just needed to run

pip3 install virtualenvwrapper
Nullipore answered 25/1, 2019 at 17:8 Comment(0)
S
12

In my case, there was somehow a mismatch between pip3 and python3 (because I have multiple pythons installed). This worked for me:

sudo python -m pip install virtualenvwrapper
Signe answered 4/4, 2020 at 5:19 Comment(0)
C
9

check your python3 installation directory:

which python3

If installed by brew you should get:

/usr/local/python3

export python version to be used virtualenvwrapper:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3

source your shell configuration file:
bash

source .bashrc

zsh

source .zshrc

Clintclintock answered 5/5, 2017 at 8:8 Comment(1)
This one helped me: my export VIRTUALENVWRAPPER_PYTHON was set to python3. Changing it to /usr/bin/python3resolved the issueBritishism
T
9

Here is what for me solved

Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')

obtained in creating and activating environments.

In my .bashrc I had wrote:

export VIRTUALENVWRAPPER_PYTHON=python3

Changing it to:

export VIRTUALENVWRAPPER_PYTHON=$(which python3)

solved the problem for me.

NOTE: If you activate an environment and run which python3, you will find out that the python run is the one of the environment, not the global one specified in VIRTUALENVWRAPPER_PYTHON

Teague answered 15/12, 2020 at 21:35 Comment(3)
Same issue on Ubuntu 20.04, this solved it. As a rule of thumb: put the full path of your python interpreter, not relative, since it changes while the virtualenv is starting.Ericerica
Wish I could up-vote this twice. I tried so. many. other. versions of this suggestion. This is the only one that worked.Reive
I was scratching my head for a couple of hours until I found this. Definitely need more upvotes. Saved a lot more hours.Glyoxaline
S
8

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python

good choice, but you already knew /usr/bin/python is 2.7

If you want to use version 3

sudo apt-get install python3-pip

and logout & login

Sporocyst answered 4/12, 2015 at 23:13 Comment(0)
A
7

This error appeared for me after running brew update and brew upgrade on mac os high sierra.

The issue was resolved by reinstalling virtualenvwrappper i.e. pip install virtualenvwrapper.

Adulate answered 3/4, 2018 at 18:20 Comment(0)
A
4

I've got this error for entirely different reasons, but since I ended up on this thread, I gather somebody might find this useful. So in my case, it turned out that my script was attempting to activate the virtual environment while I was already on it (the workon statement is issued from my .profile file). And so the solution was to simply deactivate the environment first and then run the script.

Autostrada answered 3/7, 2020 at 12:21 Comment(1)
Thank you so much, gosh it never crossed my mindColoratura
R
4

If you're here after a macOS update, and installed python3 via brew, try re-linking your version of python3

Radmen answered 14/1, 2021 at 13:11 Comment(0)
E
3

On my computer, I ran sudo apt install python-pip and then pip install virtualenvwrapper but on my settings, I set it as follows:

export WORKON_HOME=$HOME/.Envs
export VIRTUALENVWRAPPER_PYTHON=$(which -a python3)
source $HOME/.local/bin/virtualenvwrapper.sh

That got rid of the error and note that I am using python3 and I use pip3 as my default

Ellingston answered 15/5, 2020 at 9:10 Comment(0)
R
2

I had this warning after a brew upgrade, which updated python3 packages and brew-installed python3.

I found a pip3 and python3 version mismatch as Evan Thomas hinted:

❯ which python3 && python3 --version
/usr/bin/python3
Python 3.8.2

❯ which pip3 && pip3 --version
/usr/local/bin/pip3
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Running brew link [email protected] as snakesNbronies suggested removes the warning.

Ruelas answered 22/2, 2021 at 8:27 Comment(1)
You're a hero. I needed to brew link --overwrite [email protected]Preface
M
2

I found that problem can be reproduced with or without pyenv. Here checklist how to solve this problem which helps me on Mac with fresh install brew and pyenv:

  1. pyenv install 3.11.1 (or whatever version you need) and global it with command pyenv global 3.11.1
  2. type python and make sure, that it's the same version that you set-up in previous step.
  3. install virtualenvwrapper with python -m pip install virtualenv virtualenvwrapper
  4. FIRST IMPORTANT! SET THE FIRST VARIABLE CORRECT! which python tells you python path to your installed path. Use this path to selected by system python executable to tell virtualenvwrapper which python it need to use with variable VIRTUALENVWRAPPER_PYTHON.
  5. SECOND IMPORTANT! Make sure you execute correct virtualenvwrapper.sh. I use pyenv so I just use virtualenvwrapper in my global version (look for config below)
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

export PYENV_VERSION="$(pyenv version-name)"
VIRTUALENVWRAPPER_PYTHON="$HOME/.pyenv/shims/python"


export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel

source ~/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh

# Tell pyenv-virtualenvwrapper to use pyenv when creating new Python environments
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"

# Set the pyenv shims to initialize
if command -v pyenv 1>/dev/null 2>&1; then
 eval "$(pyenv init -)"
fi
Maunder answered 7/1, 2023 at 12:5 Comment(1)
Today I encountered this problem again for new user on macos 1) I just paste config to .zshrc (.bashrc in your case) 2) have next error with virtualenvwrapper. There helps to install pip install virtualenvwrapperMaunder
S
1

I had a similar issue on Mac OS Catalina 10.15.6 after I installed vim and macvim using following:

brew install vim && brew install macvim

which I believe was the cause for the problem.

While workon worked but always gave an error message as below:

 ~ $ workon loom
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
(loom)  ~ $ 

What worked for me finally was:

  1. adding export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 to .bash_profile
  2. sudo pip3 install virtualenvwrapper
Somnolent answered 8/10, 2020 at 6:23 Comment(0)
A
1

If anyone's still having this issue, I followed the top answer on this thread to fix a pip3 mismatch with my version of python, then ran sudo pip3 install virtualenv virtualenvwrapper followed by source ~/.bashrc which resolved the issue for me.

Alburnum answered 26/10, 2022 at 15:56 Comment(1)
This fixed it for me. Got the error after upgrading from Pop!_OS 20.04 to 22.04.Magness
P
0

try to install python-is-python3:

1.Update the package index:

sudo apt-get update

2.Install python-is-python3 deb package:

sudo apt-get install python-is-python3
Palaeogene answered 8/1, 2021 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.