在Pycharm中将Python2代码转换Python3

又折腾了一下午。。。
怎样在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, --helpshow this help message and exit
-d, --doctests_onlyFix up doctests only
-f FIX, --fix=FIXEach FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSESRun 2to3 concurrently
-x NOFIX, --nofix=NOFIXPrevent a transformation from being run
-l, --list-fixesList available transformations
-p, --print-functionModify the grammar so that print() is a function
-v, --verboseMore verbose logging
–no-diffsDon’t show diffs of the refactoring
-w, --writeWrite back modified files
-n, --nobackupsDon’t write backups for modified files
-o OUTPUT_DIR, --output-dir=OUTPUT_DIRPut output files in this directory instead of
-W, --write-unchanged-filesAlso write files even if no changes were required
–add-suffix=ADD_SUFFIXAppend 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值