virtualenv + virtualenvwrapper
« closures | Emacs' 24 python.el now supports ipython »
If you are a python programmer you should know what a virtualenv is. If not, then this is the right place to learn.
What's a virtualenv?
As the name suggests, it is a virtual environment for your python project. In this environment you can install different versions of python packages for different projects without taking risks of messing up with your system's site-packages or other projects!
How to create a virtualenv?
Creating a virtualenv without virtualenvwrapper is possible but no that fun. So for the first step we will be installing virtualenv *and* virtualenvwrapper.
Installing things up
- On Arch GNU/Linux: yaourt -S virtualenv python-virtualenvwrapper
- On other distros or operating systems:
- You'll need to install virtualenv (su -c 'apt-get install python-virtualenv' on Debian based distros or easy_install)
- You'll need to install virtualenvwrapper (you can grab it with easy_install virtualenvwrapper)
After you have virtualenvwrapper installed you'll need to source /usr/bin/virtualenvwrapper.sh in your .bashrc so all bash functions get installed. You can achieve this with the following bash command: echo ". /usr/bin/virtualenvwrapper.sh" >> ~/.bashrc
You will also need to create the .virtualenvs folder where all your environments will live. You can change this folder location by setting the WORKON_HOME environment variable to whatever path you want.
After all that, you have two options: close the terminal and open a new one or execute source ~/.bashrc
Creating and using our first virtualenv with virtualenvwrapper
To create a new virtualenv using our default python version but without its site-packages we execute mkvirtualenv with the --no-site-packages argument. If we wanted to set an specific python version for the environment the -p switch would do the trick (ie: mkvirtualenv -p python2.5).
[fgallina@cuca ~]$ mkvirtualenv myproject --no-site-packages
New python executable in myproject/bin/python
Installing setuptools............done.
Our environment has been created, so now we want to activate it:
[fgallina@cuca ~]$ workon myproject
(myproject)[fgallina@cuca ~]$
Notice how the prompts now starts with (myproject), this indicates the environment has been activated successfully. So there it is, we have our virtual environment. Now let's test it installing some packages on it.
(myproject)[fgallina@cuca ~]$ easy_install PIL simplejson MySQL-Python django
...Lots of logs...
Finished processing dependencies for django
(myproject)[fgallina@cuca ~]$ python
Python 2.6.5 (r265:79063, Apr 1 2010, 05:28:39)
[GCC 4.4.3 20100316 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django; django.VERSION
(1, 2, 0, 'final', 0)
Ok, so we have our packages installed, but what's the status of our main python installation packages? Well they will remain the same as the were before you installed anything on the virtualenv. To check this, let's deactivate the environment we are working on and start a python shell to check the django version our main python installation has.
(myproject)[fgallina@cuca ~]$ deactivate
[fgallina@cuca ~]$ python
Python 2.6.5 (r265:79063, Apr 1 2010, 05:28:39)
[GCC 4.4.3 20100316 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django; django.VERSION
(1, 1, 1, 'final', 0)
Bingo! nothing got broken.
So what's next?
This powerful combination of virtualenv + virtualenvwrapper can be even more cool if you start using pip and requirement files. pip is a drop-in replacement for easy_install and it has some great features, for instance it *can* uninstall packages and read a text file with python package names and versions and install them automatically. I will talk about pip on detail on the next article.
Useful links:
- virtualenvwrapper: http://www.doughellmann.com/projects/virtualenvwrapper/
- pip: http://pip.openplans.org/