If you use an editor like PyCharm that does inline code listing, it will pick up a lot of things that don’t meet the current Python 3 standard and give little inline visual indications that need to be attended to.
But a more comprehensive solution could be to run Python’s built-in
2to3. This is a script that will process your existing Python script and output a version translated for Python 3. I’m not sure how interactive it is, which may limit how useful it is in actually learning what needs to be changed, and keeping control over what is changed.
There are some useful pointers on this SO page as to how to get started with it (in this case, ignore the references to PyCharm, it is a red herring here):
To be honest, I’ve done conversions by just repeatedly running the scripts under Python 3 and dealing with the errors as they arose. As I found out the common issues, it was easy to do a find and replace to deal with them in bulk. This process was a bit laborious but kept me in control of knowing what was being changed and why.
In effect, you’ll probably be surprised by how simple it all is. A lot of it is changing
print statements into
print()functions, removing all
xrange() in favour of
range(), no longer needed to use
u'' prefixed strings, etc. If you deal with things like serial ports and other low-level text processing functions, you might find yourself having to make distinctions between when a string is a string, and when it is bytes, which can be the trickiest thing to get to grips with.
I only found one subtle bug, related to treating the result of a Python 2
range() as a simple modifiable list, while in Python 3,
range() returns an iterator.