在 PyCharm 中将 python 2 代码转换为 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37891188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert python 2 code to 3 in PyCharm
提问by loag
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?
我在 python 2 代码中有一个大型 ML 项目,我刚开始使用 PyCharm 作为 IDE。我目前使用的是 WinPython 3.4,我最好在 python 3 中做所有事情,而不是继续使用 legacy 2。 3 from 2 但我并没有真正考虑它并退出它。我如何转换它?
回答by M.Hassan
I found a way in PycharmIDE to convert file from v2 to v3 using 2to3 tool.
我在PycharmIDE 中找到了一种使用 2to3 工具将文件从 v2 转换为 v3 的方法。
I applied in pycharm comunity edition v 2016.2.3 in windows environment.
我在windows环境下申请了pycharm comunity edition v 2016.2.3。
- click terminalin status bar Now, you are in shell command, in the root of your project.
- type the command (to convert myfile.py):
- 单击状态栏中的终端现在,您处于项目根目录中的 shell 命令中。
- 输入命令(转换 myfile.py):
2to3 myfile.py -w
The tool modifies the code of the file, and your IDE is reflected with the changes.
该工具会修改文件的代码,并且您的 IDE 会通过更改反映出来。
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:
-w
实际写入更改的选项。有关更多详细信息,请写:
2to3 -h
回答by Mohan Babu
Goto the folder where your python2script.py is existing in command line,
转到命令行中存在 python2script.py 的文件夹,
then execute the command:
然后执行命令:
python C:/python/Tools/scripts/2to3.py -w python2script.py
you can see that your python2scipt.py is updated.
您可以看到您的 python2scipt.py 已更新。
回答by Talat Parwez
In order to convert your python script from version-2 to version-3, you can simply use 2to3 utility.
为了将您的 python 脚本从 version-2 转换为 version-3,您可以简单地使用 2to3 实用程序。
On linux terminal -
在 linux 终端上 -
$ 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.
其中 my_file.py 是您要转换的文件。
回答by Brendan Abel
There's a scriptincluded 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.
Python 中包含一个脚本,通常位于[Python Root]/Tools/Scripts/2to3.py
. 您可以在 python 文件(或 python 文件目录)上运行该脚本,它将处理大量转换,至少对于标准库中的更改。
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.
如果您的项目使用其他 3rd 方库,它会变得更复杂一些。在 2 到 3 转换期间更改的 API 可能会发生变化,而2to3.py
脚本将不知道这些 api 更改。最好的办法是运行转换脚本,然后手动进行所需的任何其他更改。
回答by Jaxian
Before anything I would save a backup copy of your Python 2 file first.
在做任何事情之前,我会先保存您的 Python 2 文件的备份副本。
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#
然后,您可以尝试使用“2to3”自动 Python 2 到 3 代码转换工具来转换代码,该工具通过标准库内置于 Python 中。可以在此处找到有关使用的详细信息: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/
现代化 --> https://python-modernize.readthedocs.io/en/latest/
Futurize --> http://python-future.org/automatic_conversion.html
未来化 --> 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.
就 Pycharm 而言,我在 IDE 中没有看到/不知道任何将代码从 Python 2 转换为 Python 3 的专用工具。我会坚持使用上面的 3 个工具。
Good luck!
祝你好运!