osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is your experience porting Python 2.7.x scripts to Python 3.x?


On 22Jan2019 19:20, Grant Edwards <grant.b.edwards at gmail.com> wrote:
>On 2019-01-22, Schachner, Joseph <Joseph.Schachner at Teledyne.com> wrote:
>> For anyone who has moved a substantial bunch of Python 2 to Python
>> 3, can you please reply with your experience?
>
>If you used bytes (or raw binary strings) at all (e.g. for doing
>things like network or serial protocols) you're in for a lot of pain.

Yes, but you will be the better for it afterwards. I've had a few 
programs which worked with binary data, and often also "text". In Python 
2 there was _constant_ uncertanty when these were mixed (writing text 
into binary fields and related). In Python 3 I am never confused. It is 
a huge win.

The pain here is completely offset by the relief which comes later.

>Everything else is pretty minor.

Largely. It is also possible to write a _lot_ of code compatible with 
both 2 and 3.

  from __future__ import absolute_imports, print_function

gets you a long way. It will force these 2 things on your Python 2, 
making it Python 3 ready in that regard before you cut over.

I gather the 2to3 tool is useful, but it generates _separate_ Python 3 
code from your python 2 codebase as I understand it. I don't like that 
maintenance burden. I went with portability myself for most things, and 
a small personal library of python 3 flavoured routines with python 2 
ports for some differing behaviour (because that gets me native Python 3 
performance in Python 3, post cutover - the library is basicly "import 
the Python 3 names" in Python 3, and "implement the same names in Python 
2" where needed for the Python 2).  The "six" library is apparently the 
go to one for this kind of thing, I gather.

The pain level is really pretty low. The bytes vs strings stuff is the 
most difficult, but the outcome is vastly better afterwards.

Cheers,
Cameron Simpson <cs at cskk.id.au>