Python Forum

Full Version: Resources for converting Python 2.x code to 3.x
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Resources for converting Python 2.x code to 3.x
I don't intend to use Python 2.x. I'll be writing all my projects in 3.x. However, sometimes I look up code samples to find solutions to problems. Why reinvent the wheel if someone else has already created code for a certain task and has made it available?

Of course, there's always the chance that I'll find exactly the code I need, except that it's written in 2.x. For that reason, I've looked up the differences between the two versions. I found this page:

https://docs.python.org/3.0/whatsnew/3.0.html

It doesn't appear to cover tkinter, however, so I'll be looking up a page on that too. Is there anything else I should keep in mind? Are there any other good resources in the event that I need to convert Python 2.x code to 3.x?  
For most things, the 2to3.py tool can convert code for you. So you can save the snippet you find to a temporary file, and run the tool against it to see the new version.

https://docs.python.org/2/library/2to3.html
(May-03-2017, 05:59 PM)nilamo Wrote: [ -> ]So you can save the snippet you find to a temporary file, and run the tool against it to see the new version.
2to3 make a temporary file automatically when use write 2to3 -w foo.py
After run foo.py will be the Python 3 version,and backup Python 2 version will be foo.py.bak.
Right, but if you're looking at a webpage with code on it, you still need to save that code to a temporary file... otherwise how do you run 2to3 against it? :p
Sure i was misunderstanding that part.