Skip to content

Instantly share code, notes, and snippets.

@dixneuf19
Forked from hminnovation/zsh-virtualenv-setup.md
Last active April 16, 2024 21:41
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dixneuf19/a398c08f00aac24609c3cc44c29af1f0 to your computer and use it in GitHub Desktop.
Save dixneuf19/a398c08f00aac24609c3cc44c29af1f0 to your computer and use it in GitHub Desktop.
Setup python, pip, virtualenv and virtualwrapper, with zsh on a new machine

Setup python, pip, virtualenv and virtualwrapper, with zsh on a new machine

Strongly inspired by https://gist.github.com/heymonkeyriot/9a2f429caff5c091d5429666fa080403.

Installing Python & Pip

On Ubuntu :

sudo apt install python3 python3-pip

or on Fedora < 31 :

sudo dnf install python3 python3-pip

That'll install both Python and PIP.

(Side note that you should install multiple versions of Python via Pyenv rather than Brew/apt/dnf to avoid any bad Python conflicts)

Installing Virtualenv & Virtualenvwrapper

Install all your python env into local.

pip3 install --user virtualenv virtualenvwrapper

You'll need to tell ZSH where virtualenvs scripts are located

In ~/.zshrc add the bin folder to the path before the plugins part :

# Add the bin folder to $PATH before the plugins load
PATH=$HOME/.local/bin:$PATH

Also, add the virtualenvwrapper to the plugin array :

plugins=(
        git
        ...
        virtualenvwrapper
    )

Then you can load your shell with this new config :

source .zshrc

You might get into errors :

  • Virtualenvwrapper plugin doesn't find the path of virtualenvwrapper.sh :

    [oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.
    Please install with `pip install virtualenvwrapper`

    To fix it, you can put the correct path in the virtualenvwrapper plugin. Note: I don't find these very elegant, you'v probably forgot to update your PATH before. For example, change all the lines of ~/.oh-my-zsh/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh from /usr/local/ to $HOME/.local (if you've installed locally the modules).

    You can do it with one commande line with

    sed -i "s/\/usr\/local/\$HOME\/.local/g" ~/.oh-my-zsh/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
  • The virtualenvwrapper.sh script throws an error like :

    virtualenvwrapper_run_hook:12: permission denied:-
    virtualenvwrapper.sh: There was a problem running the initialization hooks.

    It's probably your system use python3 command and not python. Adding alias python=python3 doesn't seem to work.

    You need to add in .zshrc, before the plugins line :

    export VIRTUALENVWRAPPER_PYTHON=$(which python3)
  • ERROR: Environment '/home/john_doe/.virtualenvs/test-env' does not contain an activate script. on a Raspberry Pi

    First check your version of virtualenvwrapper with pip freeze | grep virtualenv, it should be 4.x.x and not 5.0.0. If necessary downgrade your version with pip uninstall virtualenvwrapper and pip install --user virtualenvwrapper=="4.8.4" (check the last version at PyPi).

    Then you can have the same issue is the as above, so just add export VIRTUALENVWRAPPER_PYTHON=$(which python3) to your .zshrc (or .bashrc) before loading the plugin.

Use virtualenvwrapper

When the source doesn't throw any errors, you can test if virtualenvwrapper works.

workon
mkvirtualenv test

Here you should see

New python executable in /home/john_doe/.virtualenvs/test/bin/python2.7
Also creating executable in /home/john_doe/.virtualenvs/test/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/john_doe/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/john_doe/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/john_doe/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/john_doe/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/john_doe/.virtualenvs/test/bin/get_env_details

You should see a (test) somewhere in you shell, but it might depend of the theme. (By default the ZSH virtualenv plugin hides the virtualenv name with export VIRTUAL_ENV_DISABLE_PROMPT=1. That's why I didn't add it to plugins, it doesn't seem very useful but migh be linked with the theme I use.)

If you obtain the error ERROR: virtualenvwrapper could not find virtualenv in your path, check if you've add the bin folder to $PATH (see above).

Do whatever you want in your venv. Then to quit and delete.

deactivate
rmvirtualenv test

If the zsh plugin works correctly, you can link a venv to a git folder (auto workon and deactivate when you navigate in and out the folder. First create the folder test (usually a git clone). Then mkvirtualenv test. It should work, hopefully.

@rodridev
Copy link

Thank you!

@the-vampiire
Copy link

brilliant mate thanks. i couldnt figure out this issue until i found your guide

virtualenvwrapper_run_hook:12: permission denied:-
virtualenvwrapper.sh: There was a problem running the initialization hooks.

@miguelsmatos
Copy link

@the-vampiire that might happen when you do not set the python_exe before calling initializing the zsh plugin. Setting the environment variables in a different place on the .zshrc may help

@mew1033
Copy link

mew1033 commented Feb 20, 2021

Excellent, thank you!

@faynopi
Copy link

faynopi commented Feb 23, 2021

Thank you ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment