
How to Port from Python 2 to Python 3 (2019) - noego
https://stxnext.com/blog/2019/04/16/python-3-migration-guide/
======
ryandvm
Python 3 and IPv6 are the poster-children of how _not_ to do a major upgrade.
I'm not sure what the right way is, but if the short-term advantages of the
upgrade do not outweigh the immediate pain, prepare for the matter to drag out
for _decades_.

~~~
segmondy
Upgrades are just hard.

See perl5 to perl6 GWbasic to Qbasic to VB to VB.net

you either make a clean break or keep all the warts, Either way folks are
going to be unhappy.

Keep the warts, COBOL, Fortran, C, C++, PHP, Excel

~~~
Asmod4n
Ruby did a great job back in the day with their 1.8 release which changed the
language to be Unicode friendly.

~~~
mixmastamyk
What did they do differently? I guess they benefited from hindsight.

------
LoreleiPenn
Hopefully no more people will keep saying "learn Python 2 because 3 has almost
no packages".

It is so easy for people to just repeat what they heard even if that idea
originated a decade ago and was valid a decade ago.

And that way we got into a mess of not migrating until pass the time it is no
longer supported...

------
zdw
I've been doing a lot of Python 2 -> 3 lately, and found this to be one of the
best actionable guides:
[https://portingguide.readthedocs.io/en/latest](https://portingguide.readthedocs.io/en/latest)

Also, using tox on the project to run tests against both python 2.7 and
multiple versions of 3 and the work goes pretty quickly.

~~~
mixmastamyk
Looks like a lot of the guide assumes you want to run on Py2 and 3
concurrently. The time for that has passed to be honest. A clean port is
easier to do.

~~~
zdw
For larger code bases making direct jump straight to Py3 that breaks Py2
compatibility without a transitional period can be problematic especially if
it's a library.

The time may have passed, but lots of code is still out there that needs to be
updated.

~~~
mixmastamyk
Maintaining two branches shouldn’t be a problem. If you waited this long to
port, velocity on the legacy branch can’t be especially high.

Now that I think of it the legacy branch should be eol soon.

------
pjc50
We built a product with an embedded Jython interpreter. Jython is stuck on
Python2 and somewhat abandoned. So that's nice.

Re: packages, one of the huge advantages of the C ecosystem has been that
compiled packages are _usually_ fine across language transitions, not only
between major compiler version numbers but even from C to C++ which are much
more different languages than Python2 to 3. How different would the Python
transition have been if it were possible to load Python2 packages in a Python3
program?

------
o_x
Isn't it ironic that Sentry is one of the tools mentioned in py2->py3
migration? (Sentry is on py2 and as far as I remember they were not very
optimistic about migrating)

~~~
zojirushibottle
that's correct. sentry itself is on python 2.7, not the python client.

not to pick on sentry here, but you know, my experience is that people are
having a hard time migrating due to them using obscure tricks and features of
python 2.7. so their code is breaking because the language evolved.

the saying goes write dumb code or something because debugging is twice as
hard. if there is anything to learn from all this, it's to write dumb code
because maintenance is twice as hard too.

that's all that is happening really!

~~~
ggregoire
Why would you even build a logs collector in python? Especially if it’s your
core business and you know you will need scale and reliability. You kinda
shoot yourself in the foot.

~~~
baq
they're using python 2.7. perhaps started on an earlier one. maybe there was
nothing except java 1.4 when they started.

------
zitterbewegung
The big issue of ports like these is not the tutorial but to justify that to
your boss.

From enterprise to a self run startup you have to see if it’s worth it .

~~~
GrayTzar
Hi, I'm one of the STX Next content crew. We actually have a companion piece
to this that goes over the reasons why you should migrate:
[https://stxnext.com/blog/2019/07/30/why-migrate-from-
python-...](https://stxnext.com/blog/2019/07/30/why-migrate-from-python-2-to-
python-3/)

Maybe that would be useful for a conversation with one's boss.

------
swalsh
I literally just got on a phone call to discuss our migration away from 2.7,
very timely post.

------
classified
IIRC, the Python used in the macOS vim(1) is still 2.x. So at least on a Mac
it won't be possible to just move on to Py3 and forget / uninstall Py2 for the
foreseeable future.

~~~
cinnamonheart
I think you can just do

    
    
        brew install vim -- --with-override-system-vi --with-python3
    

To get a vim with python 3 on mac os. You are correct that it defaults to
python 2, though.

------
mixmastamyk
Porting is a non-event for most non-large projects. In short:

\- First cut a new major version

\- Write a few tests if needed, they go a long way here.

\- Update to 2.7 best practices and logging

\- Run tests, commit

\- Add a few future statements, commit

\- Run pyflakes3 on it, fix, commit

\- Run under 3.x/fix until clean, commit

However, if your project is huge and/or does a lot of string and bit twiddling
it's excruciating. Hence the controversy between factions.

------
grifball
sed -i 's/print \\("[^"]*"\\)/print(\1)'

