[Distutils] 2to3 and version numbers with distribute

Barry Warsaw barry at python.org
Wed Feb 8 22:45:33 CET 2012

On Feb 08, 2012, at 02:05 PM, Andrew McNabb wrote:

>I'm trying to use 2to3 in setup.py for the first time.  I have
>distribute installed, so it sounds like I should be able to just add
>"use_2to3=True" to my call to setup, and things should just work.
>Unfortunately, the setup.py is currently importing the package in order
>to determine the version number.  Specifically, setup.py has:
>from xyz import version
>setup(name='xyz', version=version.VERSION, ...)
>If I run "python3 setup.py build", this fails because the "from xyz
>import version" causes lots of python3-incompatible code to get called.
>It seems like there's probably an easy way around this that I'm missing.
>In theory, I could specify the version number in two different places
>and hope that they would stay synced, but I'm not smart enough to pull
>that off. :)  Is there anything else I should try?  Thanks.

Take a look at informational PEP 396 for some thoughts:


I'll also mention that depending on the minimum version of Python you need to
support, you might be able to do away with 2to3 and support both Python 2 and
3 from the same code base.  This is what I'm doing whenever possible (with
Python 2.6 as a minimum version), and it's not too hard to do.  Michael Foord
has some good recommendations for writing code compatible even back to 2.4.

If you can't do that, try to minimize the amount of code that "from xyz import
version" has to execute.  Maybe stick an __version__ attribute in your
xyz/__init__.py file, and nothing else.  Or put the version number in a
version.txt file and read from that in both your xyz.version module and your

Better yet, don't grab the version out of that module by importing.  Instead,
just parse the contents of the file.  You might find the get_version()
function from this little helper useful for that:



More information about the Distutils-SIG mailing list