Python Automation
Created on March 01, 2011.
I briefly described my latest web migration in a recent post. I wanted to share the little magic that goes on behind the scenes that makes it all flow very nicely. This magic isn’t at all specific to web development or to Hyde — it’s generally just cool automation.
[TOC]
Virtualenv Wrapper
First up is the venerable virtualenvwrapper by Doug Hellmann — an extension to another tool called virtualenv. virtualenv is a Python tool that allows you to make a dedicated environment to a particular project. It can do this by just extending the existing system Python environment, or my preference, using just the base Python interpreter and install all other dependencies explicitly.
Anyone that has worked with me and my coding style will know that I am
rather pedantic when it comes to dependencies. If file A depends on file B
and file C, I #include then in file A, even if file B #includes file
C. I like it to be very clear what depends on what. Likewise, I like my
Python projects to be able to explicitly list what libraries it depends
upon. Virtualenv allows you to do this with the --no-site-packages
option. You can then install the packages you need and list them in a
requirements.txt file, which can be fed into pip. Pip, of course,
being the “pip installs packages” tool, which replaces easy_install.
So, back to virtualenvwrapper, this is really the hardest part to setup (and it’s quite easy). Since I want virtualenv available to all users of my system, I simply execute:
sudo pip install virtualenv
This works on my Mac, it works on Fedora (with some tweaking of sudo) and it works on Ubuntu or any other UNIX-based OS out there. I haven’t messed with Python on Windows in recent times, but given a nice, modern distribution of Python on Windows, I bet it would work like a charm there too (except w/o sudo, maybe runas if you’re not an admin).
So, once you have virtualenv installed, you’ll also want to install virtualenvwrapper. This is accomplished the same way, with a few tweaks after install (hell, you probably could have just skipped the previous step since pip is smart enough to handle dependencies):
sudo pip install virtualenvwrapper
So…now you have virtualenvwrapper installed, you want to set it up.
You need to set the environment variable WORKON_HOME and then source
the virtualenvwrapper shell script. I recommend doing this in your
.bashrc or .profile in your user home directory (I’ve never tried
this under zsh or similar, I think it’s bash only). Just add the following
lines (with your particulars):
WORKON_HOME=$HOME/Projects/virtualenvs export WORKON_HOME source /usr/local/bin/virtualenvwrapper.sh
Complete the initial setup by running mkdir -p $WORKON_HOME. Now, what
this does is quite simple. We’re storing our virtual environments under
~/Projects/virtualenvs and we have to make that directory. The source
line adds tab-completion and some handy commands to your shell.
So, we want to start a project. Since I’m writing about this site, we’ll
call it nethedz_org. I keep my website projects under ~/Sites. Here
we go:
# Prep your working directory mkdir ~/Sites/nethedz_org cd ~/Sites/nethedz_org # Make the virtual environment mkvirtualenv --distribute --no-site-packages nethedz_org
You’ll notice that there’s nothing in your current directory. All the
virtualenv files went to $WORKON_HOME. The --distribute option tells
virtualenv that we don’t want setuptools, but rather distribute.
The --no-site-packages is my preference. Basically it says don’t use
any of the system python libraries beyond the base python distribution.
The advantage of this to me is that you know for sure what all your
dependencies are. Since I may develop code on any box connected to the
Interwebs, I like to be explicit about this.
Speaking of explicit, I list all dependencies in a file at the top of my
project called requirements.txt. This allows me to use pip very easily
to install everything I need on a clean system.
Additional automation tips that I use for this site, is I backup my code
using git and host it over at Github. If you only use what I’ve
described so far, you can follow the instructions in the README file there
to setup this site.
SIDENOTE: You’re free to look at the code and layout and CSS and all, but please don’t rip off my site. I started with another site’s code but I completely gutted it to make it my own. Please respect my efforts and do the same.
Still to Come
In my next post, I’ll describe using Fabric, one of the greatest Python tools I’ve used in some time.