Why use Virtual env wrapper and how!

Having worked on many python/Django projects over the last few years I have come to be very familiar with virtual environments as the easiest way for me to manage multiple project requirements on just one machine. Without them my main python packages folder would be constantly fighting over different versions of Django, PIL and every other library under the sun.

Virtual environments are one of the best ways to create multiple unique development environments on one machine as they keep each projects packages contained with the specific versions required all in one folder.

Once you have multiple virtual environments it starts to get a bit complicated, especially if you have to switch between multiple ones in a day. This is where virtualenvwrapper becomes so useful. virtualenvwrapper adds an extra management layer on top of the virtualenv so that you can navigate to and start the environments with a single command. It also helps to organise your environments by keeping them in one location (previously I kept environments next to the project)

I’m not going to go over the details of how to set up and run a virtualenv as it’s well documented, but I will go over how I got virtualenvwrapper working since I had problems with it.

sudo pip install virtualenv 
sudo pip install virtualenvwrapper

Haha! I lied about the virtualenv :P. We just installed it right then, but that's not important! Now let’s setup the virtualenvwrapper.

Edit your terminal config. I use ZSH and the nano editor so i do:

nano ./zshrc
If you use the default bash then it’s nano ./bashrc

Add the following anywhere (at the bottom is good)

#Set the location that virtualenvs are kept
export WORKON_HOME=$HOME/.virtualenvs
#Set the location that new projects are created using the mkproject command (this can be skipped if you won’t be using this)
export PROJECT_HOME=$HOME/Sites

#Setup some defaults for PIP
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true

#Checks for sanity!! :)
if [[ -r /usr/local/bin/virtualenvwrapper.sh ]]; then
    source /usr/local/bin/virtualenvwrapper.sh
else
    echo "WARNING: Can't find virtualenvwrapper.sh"
fi

Save and exit the file (nano is ctrl + x, and then y to save changes).

Reload your terminal config either quitting and re-opening or …

source ./zshrc

You will now see a big pile of code is output the first time…

virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /Users/[username]/.virtualenvs/get_env_details

You can now run virtualenvwrapper. To start type:

workon

Nothing will be shown because you have no environments. Let’s make an environment

mkvirtualenv test_env

You should see (test_env) in the terminal prompt. Your env is working and active!

(test_env) ctopham@MacName  ~/Sites/git/test_project_files>$

navigate to your project folder

setvirtualenvproject

This will link the folder that you are in to the environment that is running. You can specify a different folder and environment as options if you like (but doing it this way is quick and easy).

deactivate

Turn off the environment

workon

You now have a list of environments with test_env listed. For the sake of testing open a new window or navigate to another folder on your machine

workon test_env

You will now be in the (test_env) virtual environment and you will be back in your project folder!

Tips n codes:

  • workon will list the environments installed (nothing will show if you have none!)
  • mkvirtualenv [env_name] create a new environment with the name specified
  • workon [name] will start that environment. If you have linked it to the project folder then you will be taken there too.
  • setvirtualenvproject [env_path project_path] will link project and environment. If already in the active environment and project folder just do setvirtualenvproject and the paths are not needed.
  • deactivate deactivates the current environment
Published on

Discuss!

comments powered by Disqus