How to Port from Python 2 to Python 3 (2019)

  • 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_.

  • 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...

  • 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

    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.

  • 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?

  • 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)

  • 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 .

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

  • 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.

  • 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.

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

  • Here's how you can run your python2 code using python3 in one easy command:

    mv python2 python3