Convert python 2 code to 3 in PyCharm
I found a way in Pycharm IDE to convert file from v2 to v3 using 2to3 tool.
I applied in pycharm comunity edition v 2016.2.3 in windows environment.
- click terminal in status bar Now, you are in shell command, in the root of your project.
- type the command (to convert myfile.py):
2to3 myfile.py -w
The tool modifies the code of the file, and your IDE is reflected with the changes.
To modify all your files in the folder, type the command
2to3 . -w
The option -w
to actually write the changes.
For more details write:
2to3 -h
Goto the folder where your python2script.py is existing in command line,
then execute the command:
python C:/python/Tools/scripts/2to3.py -w python2script.py
you can see that your python2scipt.py is updated.
In order to convert your python script from version-2 to version-3, you can simply use 2to3 utility.
On linux terminal -
$ 2to3 my_file.py # shows output only on terminal
or
$ 2to3 -w my_file.py # overwrites the file with python-3 code
where my_file.py is the file which you want to convert.
Two methods to convert python 2.X.X to python 3.X.X
- Using Web-application
I develop a web application to convert python 2.x.x code to python 3.x.x. Here is the Automatic Python 2 to 3 converter.
Note: This web app is FREE and using this 2to3
python library.
- Using 2to3 library, Read the doc here.
Install 2to3 library
pip install 2to3
Convert myfile.py
2to3 myfile.py
This commends create a new file that contains your Python 3 code.
If you want to overwrite the myfile.py use -w argument like 2to3 myfile.py -w. Read the docs for more arguments.