[英]How to use 2to3 properly for python?
I have some code in python 2.7 and I want to convert it all into python 3.3 code.我在 python 2.7 中有一些代码,我想将它们全部转换为 python 3.3 代码。 I know 2to3 can be used but I am not sure exactly how to use it.
我知道可以使用 2to3 但我不确定如何使用它。
Install the following module which adds the 2to3 command directly to entry_points.安装以下模块,将 2to3 命令直接添加到 entry_points。
pip install 2to3
As it is written on 2to3 docs , to translate an entire project from one directory tree to another, use:正如2to3 docs所写,要将整个项目从一个目录树转换为另一个目录树,请使用:
2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
If you don't have 2to3
on your path, you can directly invoke lib2to3
:如果您的路径上没有
2to3
,则可以直接调用lib2to3
:
python -m lib2to3 directory\file.py
And as the docs (and other answers) mention, you can use some flags for more customization:正如文档(和其他答案)所提到的,您可以使用一些标志进行更多自定义:
-w
flag to enable writeback, which applies the changes to the file -w
标志启用写回,将更改应用于文件-n
to disable backups -n
禁用备份(there are a few more flags; see the docs for more information.) (还有一些标志;有关更多信息,请参阅文档。)
It's important to have a backup before running
2to3
.在运行
2to3
之前进行备份很重要。
- If you're using git, make a commit.
如果您使用的是 git,请提交。
- Otherwise, make a backup copy of your files.
否则,请备份您的文件。
First, run 2to3 in "soft mode" to see what it would actually do:首先,以“软模式”运行 2to3 以查看它实际执行的操作:
$ 2to3 /path/to/your/project
If you're happy with what it would do, you can then run 2to3 "for real":如果您对它会做什么感到满意,那么您可以“真正地”运行 2to3:
$ 2to3 --write --nobackups /path/to/your/project
And now you have properly run 2to3
:)现在您已正确运行
2to3
:)
On Windows:在 Windows 上:
python {path_to_python}\tools\scripts\2to3.py --output-dir={output_dir} -W -n {input_dir}
path_to_python
= directory where Python is installed path_to_python
= Python 安装目录
output_dir
= directory where to output the Python3 scripts output_dir
= 输出 Python3 脚本的目录
input_dir
= directory from where to read the Python2 scripts input_dir
= 读取 Python2 脚本的目录
To convert all python 2 files in a directory to 3, you simply could run $ C:\\Program Files\\Python\\Tools\\Scripts\\2to3.py -w -n.
要将目录中的所有 python 2 文件转换为 3,您只需运行
$ C:\\Program Files\\Python\\Tools\\Scripts\\2to3.py -w -n.
inside the directory that you want to translate.在您要翻译的目录中。 It would skip all the non
.py
files anyway, and convert the rest.无论如何,它会跳过所有非
.py
文件,并转换其余文件。
note : remove the -n flag, if you want the backup file too.注意:如果您也需要备份文件,请删除 -n 标志。
To convert the code from python2 to python3 first install the 2to3 package by using要将代码从 python2 转换为 python3,首先使用以下命令安装 2to3 包
pip install 2to3
Then run this command in directory where is your python code然后在您的python代码所在的目录中运行此命令
2to3 -w -n .
First install python 2to3
package :首先安装 python
2to3
包:
C:\Default> pip install 2to3
Than convert your python2
file into python3
in your new folder ie python3-version/mycode
比将您的
python2
文件转换为新文件夹中的python3
,即python3-version/mycode
C:\Default> 2to3 your_file_name.py --output-dir=python3-version/mycode -w -n
Your new python3
file can be seen in new folder ie python3-version/mycode
您的新
python3
文件可以在新文件夹中看到,即python3-version/mycode
Running it is very simple!运行起来非常简单! I am going to consider you already have it installed and explain step-by-step how to proceed after that:
我将考虑您已经安装了它,并逐步解释了之后如何继续:
eg C:\\Users\\{your_username}\\Desktop\\python2folder
例如
C:\\Users\\{your_username}\\Desktop\\python2folder
python {your_2to3.py_install_directory} -w .\\
eg in my case (win10) it would be:例如在我的情况下(win10)它将是:
python C:"\\Program Files"\\Python39\\Tools\\scripts\\2to3.py -w .\\
This is going to make the program scan the entire directory (and sub directories as well) and automatically convert everything that is written in Python2 to Python3.这将使程序扫描整个目录(以及子目录)并自动将用 Python2 编写的所有内容转换为 Python3。
-w
flag makes the script apply the changes creating new converted files. -w
标志使脚本应用更改创建新的转换文件。 So remove this you'd like to just scan and see what needs conversion (but without actually doing anything)所以删除这个你只想扫描并查看需要转换的内容(但实际上不做任何事情)
If you'd like to convert just one file instead of entire folders simply substitute .\\
for python2_file_name.py
:如果您只想转换一个文件而不是整个文件夹,只需将
.\\
替换为python2_file_name.py
:
eg python {your_2to3.py directory} -w python2_file_name.py
例如
python {your_2to3.py directory} -w python2_file_name.py
Also, by default it creates a .bak file for everything it converts.此外,默认情况下,它会为其转换的所有内容创建一个 .bak 文件。 It is highly advised to keep it this way since any conversion is prone to errors but if you'd like to disable the automatic backup you could also add the
-n
flag.强烈建议保持这种方式,因为任何转换都容易出错,但如果您想禁用自动备份,您还可以添加
-n
标志。
eg python C:"\\Program Files"\\Python39\\Tools\\scripts\\2to3.py -w -n python2_file_name.py
例如
python C:"\\Program Files"\\Python39\\Tools\\scripts\\2to3.py -w -n python2_file_name.py
3.Done! 3.完成!
The python 2to3.py file is mostly found in the directory C:/Program Files/Python/Tools/scripts if you already have python installed.如果您已经安装了 python,python 2to3.py 文件主要位于目录 C:/Program Files/Python/Tools/scripts 中。 I have python 3.6 and 2to3 is in the directory C:/Program Files/Python36/Tools/scripts.
我有 python 3.6 和 2to3 在目录 C:/Program Files/Python36/Tools/scripts 中。 To convert a certain python 2 code to python 3, go to your command promt, change the directory to C:/Program Files/Python36/Tools/scripts where the 2to3 file is found.
要将某个 python 2 代码转换为 python 3,请转到您的命令提示符,将目录更改为 C:/Program Files/Python36/Tools/scripts,其中找到 2to3 文件。 Then add the following command: python 2to3.py -w (directory to your script).
然后添加以下命令:python 2to3.py -w(脚本目录)。
eg.例如。 C:\\Program Files\\Python36\\Tools\\scripts> python 2to3.py -w C:Users\\Iykes\\desktop\\test.py.
C:\\Program Files\\Python36\\Tools\\scripts> python 2to3.py -w C:Users\\Iykes\\desktop\\test.py。
the '-w' here ensures a backup file for your file is created.此处的“-w”可确保为您的文件创建备份文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.