{"id":268,"date":"2012-11-09T18:19:12","date_gmt":"2012-11-09T17:19:12","guid":{"rendered":"http:\/\/blog.gocept.com\/?p=268"},"modified":"2012-12-07T22:09:51","modified_gmt":"2012-12-07T21:09:51","slug":"python-2-and-3-compatible-builds-with-zc-buildout","status":"publish","type":"post","link":"https:\/\/blog.gocept.com\/2012\/11\/09\/python-2-and-3-compatible-builds-with-zc-buildout\/","title":{"rendered":"Python 2 and 3 compatible builds with zc.buildout"},"content":{"rendered":"
Creating a single-source build environment with zc.buildout that works for both Python 2 and 3 is a bit of a hassle. This blog post shows how to do it for a minimal demo project.<\/p>\n
During the sprints at PyCon DE 2012<\/a>, we tried to make the upcoming 1.0 release of the nagiosplugin<\/a> library compatible with both Python 2.7 and Python 3.2. Going for a single code base (without preprocessing steps like 3to2<\/a>) was no too hard. The only thing left was a single-source zc.buildout setup suited for both Python 2.7 and 3.2. It worked out at last, but currently it needs two buildout configurations. This is a little bit kludgy. I hope that things will improve in the near future so that a single-source build environment with zc.buildout will be possible.<\/p>\n In the following, I will demonstrate the steps with a simple demo project called MultiVersion<\/cite>. It contains nothing more than a single class that is supposed to run under both Python 2 and 3. There is also a unit test to verify that the code works. We use zope.testrunner<\/a> to run the unit tests. The code\u2019s functionality is irrelevant for the examples, so I left it out. You can download the full source<\/a> if you are interested.<\/p>\n Older versions of virtualenv<\/a> are generally not suited since they ship with obsolete releases of distribute<\/cite> and pip<\/cite>. Check if the virtualenv included in your GNU\/Linux distribution is too old. Anything below 1.8 reduces the chance of success, so better install a current virtualenv locally then. Likewise, our bootstrap.py must be recent enough<\/a> to support both Python 2 and 3. The standard bootstrap.py<\/a> from python-distribute.org<\/a> does currently not work with Python 3.<\/p>\n Now we are ready to create a virtualenv in a fresh source checkout.<\/p>\n Python 3.2:<\/p>\n Python 2.7:<\/p>\n I will discuss the steps for Python 3.2 first, since main development will concentrate on newer Python versions. After that, I will describe the necessary steps to make the build environment backward compatible.<\/p>\n1. Use a recent enough virtualenv<\/h2>\n
$ virtualenv -p python3.2 .\r\nRunning virtualenv with interpreter \/usr\/bin\/python3.2\r\nNew python executable in .\/bin\/python3.2\r\nInstalling distribute.....done.\r\nInstalling pip.....done.<\/pre>\n<\/div>\n<\/div>\n
$ virtualenv -p python2.7 .\r\nRunning virtualenv with interpreter \/usr\/bin\/python2.7\r\nNew python executable in .\/bin\/python2.7\r\nNot overwriting existing python script .\/bin\/python (you must use .\/bin\/python2.7)\r\nInstalling setuptools.....done.\r\nInstalling pip.....done.<\/pre>\n<\/div>\n<\/div>\n<\/div>\n
2. Running buildout with Python 3.2<\/h2>\n