Question

I have a small project I want to try porting to Python 3 - how do I go about this?

I have made made the code run without warnings using python2.6 -3 (mostly removing .has_key() calls), but I am not sure of the best way to use the 2to3 tool.

Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!

Running 2to3 something.py outputs a diff, which isn't useful on it's own. Using the --write flag overwrites something.py and creates a backup.. It seems like I have to do..

2to3 something.py
python3.0 something.py
mv something.py.bak something.py
vim something.py
# repeat

..which is a bit round-a-bout - ideally I could do something like..

mv something.py py2.6_something.py # once

2to3 py2.6_something.py --write-file something.py
vim py2.6_something.py
# repeat
Was it helpful?

Solution

Aha, you can pipe the 2to3 output to the patch command, which can write the modified file to a new file:

mv something.py py2.6_something.py
2to3 py2.6_something.py | patch -o something.py

OTHER TIPS

2.x should be your codebase of active development, so 2to3 should really be run in a branch or temporary directory. I'm not sure why you'd want to have the 2.x and 3.x versions lying around in the same directory. distutils has a build_2to3 script that will run 2to3 on a 3.0 install.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top