Two Tricks For a More Interesting VirtualEnv

Posted Monday, January 2, 2012 at 2:48 p.m. by Chris Amico in Projects about Python

Ian Bicking's virtualenv has become one of those apps I just can't code without (or at least, I hate to code without). I have so many quick-hit, one-off, throwaway apps with so many strange and sometimes conflicting dependencies that I can't imagine how I'd get anything done if they all swam in the same soup.

This is all made better by Doug Hellman's virtualenvwrapper, which makes creating and managing virtual environments dead simple.

Given that, here are two tricks I figured out yesterday that could make things a little more interesting:

Using (Some) Global Site Packages

Part of what makes a virtualenv so useful is that it doesn't inherit any Python packages installed globally. That's also a downside in a few edge cases. One of those is Mapnik.

Mapnik is a tool for rendering maps from spatial data. I was toying with TileStache last night and couldn't get Mapnik's Python bindings to install in a virutalenv. Mapnik itself was installed globally (using homebrew), along with all its dependencies. Boost is the troublemaker here.

After hours--yes, hours--trying and failing to pip install mapnik2 and getting errors saying I didn't have boost_python installed (it is, globally), I was about to just give up and install the entire stack globally. But it made me queasy to have TileStache, gunicorn and whatever other Python libraries I needed for this one thing installed system-wide when all but one dependency could be safely left in an isolated virtualenv.

The trick here: toggleglobalsitepackages

Yes, that's a mess of concatenated words to type. There's also add2virtualenv, which adds just one library to a particular environment.

Use the global Mapnik. Source everything else locally.

Not just for CPython

I've been watching PyPy develop lately and the more I see, the more I want to try it out. But for all the reasons above, I'm a little nervous. How do dependencies work? Can I still pip install pypy-awesome-lib?

Yes, and virtualenv is the answer.

Both virtualenv and mkvirtualenv take a --python argument to set the Python interpreter for that virtualenv. Pass in pypy and you're up and running.



Comments:

Comments are closed for this post. If you still have something to say, please email me.