1

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.

The Pointer
  • 2,226
  • 7
  • 22
  • 50
  • It looks like your linked tutorial is missing the exports to point to python3. Check out this [setup guide](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment) in MDN, go to the Using Django inside a Python virtual environment. You'll see the python3 export and the default env version export. I did it on Ubuntu - not sure of the macOS differences. – DaveStSomeWhere Mar 13 '20 at 11:53
  • @DaveStSomeWhere See my edit. Any idea how to fix this? – The Pointer Mar 13 '20 at 14:04
  • Check out the answers [here](https://stackoverflow.com/questions/33216679/usr-bin-python3-error-while-finding-spec-for-virtualenvwrapper-hook-loader) - is that the same issue you are having? Also, what is your which python3 result? My export has the version - `export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.7` – DaveStSomeWhere Mar 13 '20 at 14:41
  • @DaveStSomeWhere Yes, it seems like it. I'm attempting the instructions in this https://stackoverflow.com/a/43799713/6828329 answer. The problem is, for `source .zshrc`, I get `source: no such file or directory: .zshrc` – The Pointer Mar 13 '20 at 14:50
  • @DaveStSomeWhere Mine is `which python3` gets me `/usr/local/bin/python3` – The Pointer Mar 13 '20 at 14:51
  • 2
    Ok, I followed this https://stackoverflow.com/a/43799713/6828329 answer and did `source ~/.zshrc` and then `source /usr/local/bin/virtualenvwrapper.sh` worked! – The Pointer Mar 13 '20 at 15:21
  • I then proceeded as stated here: https://realpython.com/python-virtual-environments-a-primer/ . 1. `export WORKON_HOME=$HOME/.virtualenvs` 2. `export PROJECT_HOME=$HOME/projects` 3. `source /usr/local/bin/virtualenvwrapper.sh` 4. `source ~/.bashrc` 5. `echo $WORKON_HOME` 6. `mkvirtualenv my-new-project` all works when done in this order. – The Pointer Mar 13 '20 at 15:48
  • I hope this will help others who encounter the same issues. Thank you @DaveStSomeWhere for the help. – The Pointer Mar 13 '20 at 15:49

0 Answers0