Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 2to3 manual modification

Is there a way to change python2.x source code to python 3.x manually. I guess using lib2to3 this can be done but I don't know exactly how to do this ?

like image 973
pkumar Avatar asked Mar 04 '26 07:03

pkumar


2 Answers

Yes, porting is what you are looking here.

Porting is a non-trivial task that requires making various decisions about your code. For instance, whether or not you want to maintaing backward compatibility. There is no single, universal solution to porting. The way you port depends on your specific requirements.

The best resource I have found for porting apps from Python 2 to 3 is the wiki page PortingPythonToPy3k. The page contains several approaches to porting as well as a lot of links to resources that are potentially helpful in porting work.

like image 155
jsalonen Avatar answered Mar 05 '26 19:03

jsalonen


Thanks. Here is the answer I was looking for:

from lib2to3.refactor import RefactoringTool, get_fixers_from_package
"""assume `files` to a be a list of all filenames you want to convert"""
r = RefactoringTool(get_fixers_from_package('lib2to3.fixes'))
r.refactor(files, write=True)
like image 42
pkumar Avatar answered Mar 05 '26 19:03

pkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!