0

Python2转换成Python3

 9 months ago
source link: https://xugaoxiang.com/2023/07/17/python2-to-python3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python2转换成Python3 - 迷途小书童的Note迷途小书童的Note

> 编程语言 > Python > Python基础 > Python2转换成Python3
  • windows 11 64bit

Python2Python3 在语法和某些模块的使用上是有一些差别的,对于一些用 python2 编写的历史项目,要重新启用它们,2to3 是一个不错的转换工具,也是官方提供的工具,不需要安装,可靠性也比较高,它读取 Python 2.x 源代码并应用一系列修复程序将其转换为有效的 Python 3.x 代码。

可以在终端中查看命令支持的所有参数



  1. (base) PS C:\Users\xgx> 2to3.exe -h
  2. Usage: 2to3 [options] file|dir ...
  3. Options:
  4. -h, --help show this help message and exit
  5. -d, --doctests_only Fix up doctests only
  6. -f FIX, --fix=FIX Each FIX specifies a transformation; default: all
  7. -j PROCESSES, --processes=PROCESSES
  8. Run 2to3 concurrently
  9. -x NOFIX, --nofix=NOFIX
  10. Prevent a transformation from being run
  11. -l, --list-fixes List available transformations
  12. -p, --print-function Modify the grammar so that print() is a function
  13. -e, --exec-function Modify the grammar so that exec() is a function
  14. -v, --verbose More verbose logging
  15. --no-diffs Don't show diffs of the refactoring
  16. -w, --write Write back modified files
  17. -n, --nobackups Don't write backups for modified files
  18. -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
  19. Put output files in this directory instead of
  20. overwriting the input files. Requires -n.
  21. -W, --write-unchanged-files
  22. Also write files even if no changes were required
  23. (useful with --output-dir); implies -w.
  24. --add-suffix=ADD_SUFFIX
  25. Append this string to all output filenames. Requires
  26. -n if non-empty. ex: --add-suffix='3' will generate
  27. .py3 files.

下面看一个具体的示例,这是 python2 编写的代码,保存成文件 test.py



  1. def greet(name):
  2. print "Hello, {0}!".format(name)
  3. print "What's your name?"
  4. name = raw_input()
  5. greet(name)

执行转换命令



  1. 2to3.exe -w test.py

可以看到,转换过程中会输出2个版本的差异,然后将转换后的内容写入原文件中(即覆盖),而原文件内容会存放在 test.py.bak 中进行备份



  1. (base) PS C:\Users\xgx\Desktop> 2to3.exe -w test.py
  2. RefactoringTool: Skipping optional fixer: buffer
  3. RefactoringTool: Skipping optional fixer: idioms
  4. RefactoringTool: Skipping optional fixer: set_literal
  5. RefactoringTool: Skipping optional fixer: ws_comma
  6. RefactoringTool: Refactored test.py
  7. --- test.py (original)
  8. +++ test.py (refactored)
  9. @@ -1,5 +1,5 @@
  10. def greet(name):
  11. - print "Hello, {0}!".format(name)
  12. -print "What's your name?"
  13. -name = raw_input()
  14. + print("Hello, {0}!".format(name))
  15. +print("What's your name?")
  16. +name = input()
  17. greet(name)
  18. RefactoringTool: Files that were modified:
  19. RefactoringTool: test.py

如果是文件夹批量转换,命令直接跟上源码文件夹即可,如



  1. 2to3.exe -w D:\project

指定输出文件夹,使用 -o 参数;不想要 python2 的备份,使用 -n 参数


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK