如何为python正确使用2to3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20458011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:29:35  来源:igfitidea点击:

How to use 2to3 properly for python?

pythonpython-2.7python-3.3python-2to3

提问by GhostFrag1

I have some code in python 2.7 and I want to convert it all into python 3.3 code. I know 2to3 can be used but I am not sure exactly how to use it.

我在 python 2.7 中有一些代码,我想将它们全部转换为 python 3.3 代码。我知道可以使用 2to3 但我不确定如何使用它。

采纳答案by Faruk Sahin

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

回答by Nuhman

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.inside the directory that you want to translate. It would skip all the non .pyfiles anyway, and convert the rest.
note: remove the -n flag, if you want the backup file too.

要将目录中的所有 python 2 文件转换为 3,您只需$ C:\Program Files\Python\Tools\Scripts\2to3.py -w -n.在要翻译的目录中运行即可。.py无论如何,它会跳过所有非文件,并转换其余文件。
注意:如果您也需要备份文件,删除 -n 标志。

回答by Yidana Isaac

The python 2to3.py file is mostly found in the directory C:/Program Files/Python/Tools/scripts if you already have python installed. I have python 3.6 and 2to3 is in the directory 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. Then add the following command: python 2to3.py -w (directory to your script).

如果您已经安装了 python,python 2to3.py 文件主要位于目录 C:/Program Files/Python/Tools/scripts 中。我有 python 3.6 和 2to3 在目录 C:/Program Files/Python36/Tools/scripts 中。要将某个 python 2 代码转换为 python 3,请转到您的命令提示符,将目录更改为 C:/Program Files/Python36/Tools/scripts,其中找到 2to3 文件。然后添加以下命令: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”可确保为您的文件创建备份文件。

回答by szdrnja

On Windows:

在 Windows 上:

python {path_to_python}\tools\scriptsto3.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 脚本的目录

回答by Graham

If you don't have 2to3on 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:

正如文档(和其他答案)所提到的,您可以使用一些标志进行更多自定义:

  • the -wflag to enable writeback, which applies the changes to the file
  • the -nto disable backups
  • -w启用写回的标志,它将更改应用于文件
  • -n以禁用备份

(there are a few more flags; see the docs for more information.)

(还有一些标志;有关更多信息,请参阅文档。)

回答by aude

It's very important to have a backup before running 2to3.

  1. If you're using git, make a commit.
  2. Otherwise, make a backup copy of your files.

在运行之前进行备份非常重要2to3

  1. 如果您使用的是 git,请提交。
  2. 否则,请备份您的文件。

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:)

回答by Shivam Bharadwaj

First install python 2to3package :

首先安装python2to3包:

C:\Default> pip install 2to3

Than convert your python2file into python3in your new folder i.e. python3-version/mycode

比将您的python2文件转换python3为您的新文件夹,即python3-version/mycode

C:\Default> 2to3 your_file_name.py --output-dir=python3-version/mycode -w -n

Your new python3file can be seen in new folder i.e. python3-version/mycode

您的新python3文件可以在新文件夹中看到,即python3-version/mycode