Monday, September 28, 2009

Mercurial, Subversion, and virtualenv

I do enjoy using Mercurial. My only complaint has been when using it inside of a virtualenv, and using the hgsubversion extension. Every single command I ran, I would get this message:

*** failed to import extension hgext.subversion from ~/dvcs/hgsubversion/hgsubversion: No module named svn

That gets annoying, especially since if I ran it outside of a virtualenv, the command would work without problem. The fundamental issue comes down to this: I did not want to use the system site-packages in my virtualenvs, and that's where the svn SWIG bindings get installed. The idea of rebuilding svn for every virtualenv holds extremely little appeal as well.

Fortunately, there is a better way, and it is in part due to the work of Doug Hellman on the virtualenvwrapper tool. These steps could be done without that tool, but why bother? The tool makes life so much easier for it.

Before doing anything else, get the svn module working, such that you can do this from a python prompt, and get it to work. My examples are for Ubuntu 9.04, but simply changing the paths appropriately should work for you.

From the python prompt:

   >>> import svn
   >>> print svn.__file__

Once that works, you will get a path that looks like this: "/var/lib/python-support/python2.6/svn/__init__.pyc"

This means that the path for the module I need to symlink is "/var/lib/python-support/python2.6/svn". The question becomes where to place the link, so that I will only bring in the svn module, without all the other packages in the system site-packages?

I happen to store my extensions in a directory named $HOME/dvcs , so the best place for this is there. YMMV. With that in mind, I do the following:

   $ mkdir $HOME/dvcs/svnpkg
   $ ln -s /var/lib/python-support/python2.6/svn $HOME/dvcs/svnpkg/svn

Now, thanks to the virtualenvwrapper I mentioned above, I can do this, and get rid of that error message once and for all:

   $ workon tg21
   (tg21) $ add2virtualenv $HOME/dvcs/svnpkg

And that's it. hgsubversion will now work cleanly inside of my virtualenvironment.

No comments: