2to3 and version numbers with distribute

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

2to3 and version numbers with distribute

Andrew McNabb
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.

--
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868
_______________________________________________
Distutils-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/distutils-sig
Reply | Threaded
Open this post in threaded view
|

Re: 2to3 and version numbers with distribute

Barry Warsaw
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:

http://www.python.org/dev/peps/pep-0396/

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
setup.py.

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:

http://bazaar.launchpad.net/~barry/flufl.enum/trunk/view/head:/setup_helpers.py

HTH,
-Barry
_______________________________________________
Distutils-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/distutils-sig
Reply | Threaded
Open this post in threaded view
|

Re: 2to3 and version numbers with distribute

Andrew McNabb
On Wed, Feb 08, 2012 at 04:45:33PM -0500, Barry Warsaw wrote:
>
> Take a look at informational PEP 396 for some thoughts:
>
> http://www.python.org/dev/peps/pep-0396/

That was a very interesting read.  If distribute provided a get_version
function as described in the PEP, I would be very excited.  It's a
little obnoxious to include a custom function, although the example from
setup_helpers.py that you provided doesn't look too bad.  Are there any
plans to include some sort of robust get_version function in distribute
and distutils2?


> 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.

I've actually taken this approach for two or three projects in the past,
and it's worked out well for me.  With this particular project, I was
hoping to keep compatibility with 2.5, but I might consider limiting
support to 2.6.  Getting exception handling to work in both Python 2.5
and Python 3 is unacceptably ugly to me, though I'm impressed that it's
possible.


> 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
> setup.py.
>
> 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:
>
> http://bazaar.launchpad.net/~barry/flufl.enum/trunk/view/head:/setup_helpers.py

Thanks for the comments.  You've given me some good ideas, though I'm
still a bit disappointed that there doesn't seem to be a standard
solution for this.

--
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868
_______________________________________________
Distutils-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/distutils-sig