2025-03-14 21:59:59 -06:00
|
|
|
|
# 2to3
|
|
|
|
|
|
|
|
|
|
|
|
> 自动将 Python 2 代码转换成 Python 3.
|
|
|
|
|
|
> 自 3.11 起,该模块已被弃用,并自 3.13 起被移除。
|
|
|
|
|
|
> 参考请见:<https://github.com/python/cpython/blob/8d42e2d915c3096e7eac1c649751d1da567bb7c3/Doc/whatsnew/3.13.rst?plain=1#L188>.
|
|
|
|
|
|
> 更多信息:<https://manned.org/2to3>.
|
|
|
|
|
|
|
|
|
|
|
|
- 显示将执行的变动但不执行(dry-run):
|
|
|
|
|
|
|
|
|
|
|
|
`2to3 {{路径/到/文件.py}}`
|
|
|
|
|
|
|
|
|
|
|
|
- 将 Python 2 文件转化为 Python 3:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-w|--write]}} {{路径/到/文件.py}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
|
|
- 将 Python 2 语言特性转化为 Python 3:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-w|--write]}} {{路径/到/文件.py}} {{[-f|--fix]}} {{raw_input}} {{[-f|--fix]}} {{print}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
|
|
- 除了某个语言特性外将所有其他的 Python 2 语言特性转化为 Python 3:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-w|--write]}} {{路径/到/文件.py}} {{[-x|--nofix]}} {{has_key}} {{[-x|--nofix]}} {{isinstance}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
|
|
- 列出 Python 2 所有可转化为 Python 3 的语言特性:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-l|--list-fixes]}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
|
|
- 将某一文件夹的所有 Python 2 文件转化为 Python 3:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-o|--output-dir]}} {{路径/到/Python 3 目录}} {{[-W|--write-unchanged-files]}} {{[-n|--nobackups]}} {{路径/到/Python 2 目录}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
|
|
- 使用多线程运行 2to3:
|
|
|
|
|
|
|
2025-12-16 10:20:31 -07:00
|
|
|
|
`2to3 {{[-j|--processes]}} {{4}} {{[-o|--output-dir]}} {{路径/到/Python 3 目录}} {{[-w|--write]}} {{[-n|--nobackups]}} --no-diffs {{路径/到/Python 2 目录}}`
|