I'm trying to complete the section Managing Virtual Environments With virtualenvwrapper here.
I'm up to this section:
Once it’s installed, we’ll need to activate its shell functions. We can do this by running source on the installed virtualenvwrapper.sh script. When you first install it with pip, the output of the installation will tell you the exact location of virtualenvwrapper.sh. Or you can simply run the following:
$ which virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh
Using that path, add the following three lines to your shell’s startup file. If you’re using the Bash shell, you would place these lines in either the
~/.bashrc
file or the~/.profile
file. For other shells, like zsh, csh, or fish, you would need to use the startup files specific to that shell. All that matters is that these commands are executed when you log in or open a new shell:
export WORKON_HOME=$HOME/.virtualenvs # Optional
export PROJECT_HOME=$HOME/projects # Optional
source /usr/local/bin/virtualenvwrapper.sh
When using which virtualenvwrapper.sh
, I got the following result:
/usr/local/bin/virtualenvwrapper.sh
Then I use export WORKON_HOME=$HOME/.virtualenvs
and export PROJECT_HOME=$HOME/projects
with no output, implying success.
And finally, I use source /usr/local/bin/virtualenvwrapper.sh
and get the following result:
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: 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/python and that PATH is set properly.
I'm not sure precisely what is wrong here, but it should be using Python 3 instead of Python 2:
python --version
results in Python 2.7.16
python3 --version
results in Python 3.7.6
My OS is macOS, and my shell is zsh.
Edit
As suggested in the comments, I followed the Using Django inside a Python virtual environment section here.
This works fine:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export PROJECT_HOME=$HOME/Devel
However, source /usr/local/bin/virtualenvwrapper.sh
results in
/Library/Developer/CommandLineTools/usr/bin/python3: 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/bin/python3 and that PATH is set properly.