又折腾了一下午。。。
怎样在Pycharm中将Python2代码转换Python3?
我在 Pycharm IDE中 找到了一种 使用2to3工具将文件从v2转换为v3的方法。
我在Windows环境中的pycharm社区版v 2016.2.3中应用了。
- 在状态栏中 单击 终端 现在,您在项目根目录下的shell命令中。
- 键入命令(转换myfile.py):
2to3 myfile.py -w
该工具会修改文件的代码,并且更改会反映出您的IDE。
要修改文件夹中的所有文件,请键入以下命令
2to3 . -w
-w
实际写入更改 的选项 。 有关更多详细信息,请写:
2to3 -h
Usage: 2to3 [options] file|dir ...
Options:
-h, --help show this help message and exit
-d, --doctests_only Fix up doctests only
-f FIX, --fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a transformation from being run
-l, --list-fixes List available transformations
-p, --print-function Modify the grammar so that print() is a function
-v, --verbose More verbose logging
--no-diffs Don't show diffs of the refactoring
-w, --write Write back modified files
-n, --nobackups Don't write backups for modified files
-o OUTPUT_DIR, --output-dir=OUTPUT_DIR
Put output files in this directory instead of
overwriting the input files. Requires -n.
-W, --write-unchanged-files
Also write files even if no changes were required
(useful with --output-dir); implies -w.
--add-suffix=ADD_SUFFIX
Append this string to all output filenames. Requires
-n if non-empty. ex: --add-suffix='3' will generate
.py3 files.
选项:
参数 | 方法说明 |
---|---|
-h, --help | show this help message and exit |
-d, --doctests_only | Fix up doctests only |
-f FIX, --fix=FIX | Each FIX specifies a transformation; default: all |
-j PROCESSES, --processes=PROCESSES | Run 2to3 concurrently |
-x NOFIX, --nofix=NOFIX | Prevent a transformation from being run |
-l, --list-fixes | List available transformations |
-p, --print-function | Modify the grammar so that print() is a function |
-v, --verbose | More verbose logging |
–no-diffs | Don’t show diffs of the refactoring |
-w, --write | Write back modified files |
-n, --nobackups | Don’t write backups for modified files |
-o OUTPUT_DIR, --output-dir=OUTPUT_DIR | Put output files in this directory instead of |
-W, --write-unchanged-files | Also write files even if no changes were required |
–add-suffix=ADD_SUFFIX | Append this string to all output filenames. Requires -n if non-empty. ex: --add-suffix=‘3’ will generate .py3 files. |
谷歌翻译自git - Convert python 2 code to 3 in PyCharm - Stack Overflow