Using lib2to3 in setup.py
Friday, March 26, 2010
It seems that many people think you need distribute if you want to run 2to3 automatically in your setup.py
. But personally I don't like setuptools (aka distribute) and hence don't like forcing this on users. No worries since plain old distutils supports this as well, but it simply appears less well known.
All you need to do is use the build_py_2to3
command supplied by distutils.command.build_py
in python3 instead of the normal build_py
command. This is how you can do this:
try:
from distutils.command.build_py import build_py_2to3
except ImportError:
pass
COMMANDS = {}
if sys.version_info[0] == 3:
COMMANDS['build_py'] = build_py_2to3
setup(..., cmdclass=COMMANDS, ...)
That's it! Ok, you have to do slighty more then just add a keyword argument to the setup()
call. But just the 2to3 feature is not worth using distribute for.
2 comments:
Anonymous said...
distribute does much more that that. It makes sure your doctests are also translated and makes the whole process transparent for users who install your project.
But these features are going into distutils2 too, so if you don't like setuptools, you will be able to benefit from these features soon in distutils2
Lennart Regebro said...
And it makes testing under Python 2 and 3 easier.
New comments are not allowed.