Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips on upgrading to python 3.0? [duplicate]

So with the final releases of Python 3.0 (and now 3.1), a lot of people are facing the worry of how to upgrade without losing half their codebase due to backwards incompatibility.

What are people's best tips for avoiding the many pitfalls that will almost-inevitably result from switching to the next-generation of python?

Probably a good place to start is "use 2to3 to convert your python 2.x code to 3.x" :-)

like image 353
10 revs, 7 users 53% Avatar asked Mar 01 '23 14:03

10 revs, 7 users 53%


1 Answers

First, this question is very similar to How are you planning on handling the migration to Python 3?. Check the answers there.

There is also a section in the Python Wiki about porting applications to Python 3.x

The Release Notes for python 3.0 contains a section about porting. I'm quoting the tips there:

  1. (Prerequisite:) Start with excellent test coverage.
  2. Port to Python 2.6. This should be no more work than the average por from Python 2.x to Python 2.(x+1). Make sure all your tests pass.
  3. (Still using 2.6:) Turn on the -3 command line switch. This enables warnings about features that will be removed (or change) in 3.0. Run your test suite again, and fix code that you get warnings about until there are no warnings left, and all your tests still pass.
  4. Run the 2to3 source-to-source translator over your source code tree. (See 2to3 - Automated Python 2 to 3 code translation for more on this tool.) Run the result of the translation under Python 3.0. Manually fix up any remaining issues, fixing problems until all tests pass again.

It is not recommended to try to write source code that runs unchanged under both Python 2.6 and 3.0; you’d have to use a very contorted coding style, e.g. avoiding print statements, metaclasses, and much more. If you are maintaining a library that needs to support both Python 2.6 and Python 3.0, the best approach is to modify step 3 above by editing the 2.6 version of the source code and running the 2to3 translator again, rather than editing the 3.0 version of the source code.

like image 102
Manuel Ceron Avatar answered Mar 11 '23 10:03

Manuel Ceron