Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

porting modules to Python 3.0

3 views
Skip to first unread message

Michele Simionato

unread,
Dec 3, 2008, 12:23:37 AM12/3/08
to
Python 3.0 is going to be released Real Soon now and I realized
that there big holes in my understanding of how libraries
working both with Python 2.X and Python 3.X should be written.
Let me consider pure Python libraries for the moment.
I see various possibilities:

1. I just release two different libraries, say lib2X.py and lib3X.py,
the first for the 2.X series and the second for the 3.X series,
and
the source code of lib3X.py is generated from lib2X.py via 2to3;
2. I release a single library lib.py which consists in a big if:

$ cat lib.py
if sys.version >= '3':
from lib3X import *
else:
from lib2X import *

I am not really happy with any of the options. I think there should be
some cooperation
between PyPI and easy_install such than when the user type

$ easy_install lib

the user gets the 2.X version or the 3.X version according to the
Python version she is using.
Is there something like that already in place? What are the
recommendations for library
authors willing to support both Python 2.X and 3.X in parallel?

Michele Simionato

"Martin v. Löwis"

unread,
Dec 3, 2008, 1:21:17 AM12/3/08
to Michele Simionato
> Is there something like that already in place? What are the
> recommendations for library
> authors willing to support both Python 2.X and 3.X in parallel?

My recommendation is to use 3.0's build_py_2to3 implementation of
the build_py command. See Demo/distutils/test2to3.

You will have a single lib.py, written in 2.x. When you install
in 3.0, lib2to3 will convert it to 3.x in the build area, and
then install the 3.0 version.

That, of course, requires you to adjust lib.py in such a way that
2to3 will successfully and completely convert it. In my experience
(with porting Django) and Mark Hammond's experience (with porting
PythonWin), this should be always possible. You look at what 2to3
does, find out what additional modifications need to be done, and
apply them to the input of 2to3 so that
a) 2to3 leaves these changes in place
b) they either have no effect or still work correctly when run
in 2.x.

Regards,
Martin

Terry Reedy

unread,
Dec 3, 2008, 2:07:53 AM12/3/08
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list

In one has both 2.x and 3.0 installed, would it easy to install 'lib.py'
for both?

"Martin v. Löwis"

unread,
Dec 3, 2008, 3:02:21 AM12/3/08
to
> In one has both 2.x and 3.0 installed, would it easy to install 'lib.py'
> for both?

It's currently not possible to install something for 2.x; you have to
specifically install it for every value of x (e.g. 2.5 or 2.6).

It's the same for 3.0: you have to install it separately.

Doing so is fairly easy. You just run "setup.py install" multiple
times (or download and install prebuilt binaries if available).

Regards,
Martin

Terry Reedy

unread,
Dec 3, 2008, 11:42:09 AM12/3/08
to pytho...@python.org
Martin v. Löwis wrote:
>> In one has both 2.x and 3.0 installed, would it easy to install 'lib.py'
>> for both?
>
> It's currently not possible to install something for 2.x; you have to
> specifically install it for every value of x (e.g. 2.5 or 2.6).

That is what I meant.

> It's the same for 3.0: you have to install it separately.
> Doing so is fairly easy. You just run "setup.py install" multiple
> times

In the right place, with the right option to choose a different Python
each time. I eventually found the apparent answers in Installing Python
Modules, but only after reading well past 'the above command is
everything you need to get out of this manual' ;-(.

Hitherto, I have only unzipped library packages directly into the
desired site-packages directory. This has been easier and safer than
setup.py, but will not automatically run 2to3 over the package. Hence
my question.

Terry

0 new messages