# Setup python, pip, virtualenv and virtualwrapper, with zsh on a new machine Strongly inspired by . ## Installing Python & Pip On Ubuntu : ```bash sudo apt install python3 python3-pip ``` or on Fedora < 31 : ```bash 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. ```bash 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 : ```bash # Add the bin folder to $PATH before the plugins load PATH=$HOME/.local/bin:$PATH ``` Also, add the `virtualenvwrapper` to the *plugin* array : ```bash plugins=( git ... virtualenvwrapper ) ``` Then you can load your shell with this new config : ```bash source .zshrc ``` You might get into errors : - Virtualenvwrapper plugin doesn't find the path of `virtualenvwrapper.sh` : ```bash [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 ```bash sed -i "s/\/usr\/local/\$HOME\/.local/g" ~/.oh-my-zsh/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh ``` - The `virtualenvwrapper.sh` script throws an error like : ```bash 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 : ```bash 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](https://pypi.org/project/virtualenvwrapper/#history)). 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. ```bash workon mkvirtualenv test ``` Here you should see ```bash 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. ```bash 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.