34 lines
1.2 KiB
Markdown
34 lines
1.2 KiB
Markdown
# 2to3
|
||
|
||
> 自动化 Python 2 到 3 的代码转换。
|
||
> 此模块自 3.11 版本起已被弃用,并自 3.13 版本起被移除。
|
||
> 参考链接:<https://github.com/python/cpython/blob/8d42e2d915c3096e7eac1c649751d1da567bb7c3/Doc/whatsnew/3.13.rst?plain=1#L188>。
|
||
> 更多信息:<https://manned.org/2to3>。
|
||
|
||
- 显示将要执行的更改,而不执行它们(干运行):
|
||
|
||
`2to3 {{path/to/file.py}}`
|
||
|
||
- 将 Python 2 文件转换为 Python 3:
|
||
|
||
`2to3 --write {{path/to/file.py}}`
|
||
|
||
- 将特定的 Python 2 语言特性转换为 Python 3:
|
||
|
||
`2to3 --write {{path/to/file.py}} --fix {{raw_input}} --fix {{print}}`
|
||
|
||
- 将所有 Python 2 语言特性转换为 Python 3,除了指定的特性:
|
||
|
||
`2to3 --write {{path/to/file.py}} --nofix {{has_key}} --nofix {{isinstance}}`
|
||
|
||
- 列出所有可以从 Python 2 转换为 Python 3 的可用语言特性:
|
||
|
||
`2to3 --list-fixes`
|
||
|
||
- 将目录中的所有 Python 2 文件转换为 Python 3:
|
||
|
||
`2to3 --output-dir {{path/to/python3_directory}} --write-unchanged-files --nobackups {{path/to/python2_directory}}`
|
||
|
||
- 使用多个线程运行 2to3:
|
||
|
||
`2to3 --processes {{4}} --output-dir {{path/to/python3_directory}} --write --nobackups --no-diff {{path/to/python2_directory}}` |