[slucidi@io]# less virtualenvwrapper.html_

Python is distributed as part of the base distribution for most UNIX-like operating systems out there, including OS X. The default install is great for built in system packages and quick scripts, but you don't usually get to choose the version of the system Python, and you usually need root permission in order to install new packages from the Python Package Index. Even if you don't mind installing new packages as root, it also means that you are locked into just one version of each installed package, which can make managing dependencies for different projects tough.

To make it easier to install and manage different versions of Python and its packages, many Pythonistas recommend the use of virtualenvs. virtualenv is a fantastic tool that lets you create sandboxed environments for each of your projects, and each environment can have its own version of Python and its own set of packages. Getting it set up can be a little confusing the first time, so here's the general process:

Note that this guide assumes you use a bash-compatible shell. Things are a little different if you are using something like, say, fish

  1. Use pip to install virtualenv and virtualenvwrapper. You'll probably need to use sudo or otherwise give yourself root permissions to do this, since you're installing these tools with the system Python.

    (If your system doesn't have pip installed or you are used to easy_install, I'd recommend first installing it with easy_install pip, and then never using easy_install again. (easy_install is obsolete, and besides: pip cleans up after itself quite a bit better and is better for dealing with versioning.)

    $ sudo pip install virtualenv
    $ sudo pip install virtualenvwrapper
                        
  2. Now, source the virtualenvwrapper.sh script that pip installed earlier in your .bashrc or other shell configuration. This adds a bunch of handy management functions to your shell. Adding this line will do the trick in bash or zsh.

    $ source /usr/local/bin/virtualenvwrapper.sh
                        

At this point, you're good to go with virtualenv. Create one for your to experiment with using the mkvirtualenv command that the wrapper set up for you. You'll wind up with a prompt telling you which environment you're working on. Note that it adds a helpful identifier to your prompt to tell you which environment you are in.


      $ mkvirtualenv sandbox
      $ workon sandbox
      (sandbox) $
                    

You can jump out of your virtualenv with deactivate. From now on, typing workon sandbox will put you into the environment you created, and deactivate will take you out. Same goes for any more environments you create with mkvirtualenv. Conveniently, your shell will tab complete your virtualenv names after you type workon.

By default, new virtualenvs will use your system default Python. If you want to use different version of Python in each of your virtualenvs, that's easy too. Just pass the -p flag to mkvirtualenv with the path to the version you want, and all work you do in that environment will be with the selected Python version.


      $ mkvirtualenv -p /usr/bin/python3.3 sandbox3
      $ workon sandbox3
      (sandbox3) $ python --version
      Python 3.3.3
                    

Each virtualenv will keep its own package cache and use its own copy of pip. This is convenient because it allows you to keep different sets of packages with different versions separate from each other in order to accomodate disparate packages with conflicting dependencies.

: