porting modules to Python 3.0

  • Thread starter Michele Simionato
  • Start date
M

Michele Simionato

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
 
M

Martin v. Löwis

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
 
T

Terry Reedy

Martin said:
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

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

Martin v. Löwis

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
 
T

Terry Reedy

Martin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top