Convert python 2 code to 3 in PyCharm
Question:
I have a large ML project in python 2 code and I just started using PyCharm as an IDE. I’m currently using WinPython 3.4 and I’d preferably like to do everything in python 3 instead of continue using legacy 2. When I cloned the project from git a popup in pycharm came up that was something along the lines of converting the code to 3 from 2 but I didn`t really think about it and exited it. How do I convert it?
Answers:
There’s a script included with Python, usually at [Python Root]/Tools/Scripts/2to3.py
. You can run that script on a python file (or directory of python files) and it will handle a lot of the conversions, at least for changes in the standard library.
It gets a little more complicated if your project uses other 3rd party libraries. It’s possible the API’s for those changed during the 2-to-3 transition, and the 2to3.py
script will not know about those api changes. Your best bet is to run the conversion script, and then manually make any other changes that are needed.
Before anything I would save a backup copy of your Python 2 file first.
You can then try to convert the code using the “2to3” Automated Python 2 to 3 code translation tool which is built into Python via the Standard Library.
Details on usage can be found here:
https://docs.python.org/2/library/2to3.html#
You also have the choice between two tools to port your code automatically: Modernize and Futurize. Check them out below.
Modernize –> https://python-modernize.readthedocs.io/en/latest/
Futurize –> http://python-future.org/automatic_conversion.html
In terms of Pycharm, I have not seen / don’t know of any specialized tool within the IDE that converts code from Python 2 to Python 3. I’d stick to the 3 tools above.
Good luck!
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.
You can use this free online tool for quickly converting few files
If you also want to use it within the PyCharm IDE, you can add it as an external tool:
File
-> Settings
-> Tools
-> External Tools
and press the +
button.
A new window opens, give a name to your tool (e.g. 2to3) and a description.
In Program
add the path to the script, if you use Linux, it should be located in /usr/bin/2to3-2.7
In the Arguments
line, add $FileName$
if you want your tool to work for the current open file, or .
on the current directory, and -w
to apply the changes as seen in the other replies.
Add $FileDir$
in the Working directory
line.
Press OK and in an open file, go to Tools
-> External Tools
-> 2to3
to run the script.
I have a large ML project in python 2 code and I just started using PyCharm as an IDE. I’m currently using WinPython 3.4 and I’d preferably like to do everything in python 3 instead of continue using legacy 2. When I cloned the project from git a popup in pycharm came up that was something along the lines of converting the code to 3 from 2 but I didn`t really think about it and exited it. How do I convert it?
There’s a script included with Python, usually at [Python Root]/Tools/Scripts/2to3.py
. You can run that script on a python file (or directory of python files) and it will handle a lot of the conversions, at least for changes in the standard library.
It gets a little more complicated if your project uses other 3rd party libraries. It’s possible the API’s for those changed during the 2-to-3 transition, and the 2to3.py
script will not know about those api changes. Your best bet is to run the conversion script, and then manually make any other changes that are needed.
Before anything I would save a backup copy of your Python 2 file first.
You can then try to convert the code using the “2to3” Automated Python 2 to 3 code translation tool which is built into Python via the Standard Library.
Details on usage can be found here:
https://docs.python.org/2/library/2to3.html#
You also have the choice between two tools to port your code automatically: Modernize and Futurize. Check them out below.
Modernize –> https://python-modernize.readthedocs.io/en/latest/
Futurize –> http://python-future.org/automatic_conversion.html
In terms of Pycharm, I have not seen / don’t know of any specialized tool within the IDE that converts code from Python 2 to Python 3. I’d stick to the 3 tools above.
Good luck!
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.
You can use this free online tool for quickly converting few files
If you also want to use it within the PyCharm IDE, you can add it as an external tool:
File
-> Settings
-> Tools
-> External Tools
and press the +
button.
A new window opens, give a name to your tool (e.g. 2to3) and a description.
In Program
add the path to the script, if you use Linux, it should be located in /usr/bin/2to3-2.7
In the Arguments
line, add $FileName$
if you want your tool to work for the current open file, or .
on the current directory, and -w
to apply the changes as seen in the other replies.
Add $FileDir$
in the Working directory
line.
Press OK and in an open file, go to Tools
-> External Tools
-> 2to3
to run the script.