随着Python 3.x版本的发布和Python 2.x的停止支持,许多开发者面临一个重要的任务:将他们的Python 2.x代码转换为Python 3.x代码。虽然这项工作可能看起来繁琐且耗时,但幸运的是,有一些自动化工具可以帮助我们完成这个任务。在本篇博客中,我将介绍几种常用的方法,帮助您自动将Python 2.x代码转换为Python 3.x代码。
1. 使用 Python 2to3 工具:
- Python官方提供了一个名为2to3的工具,专门用于将Python 2.x代码转换为Python 3.x代码。Python 2to3 工具可以自动检测并转换源代码中的不兼容语法和函数调用。以下是使用2to3工具的简单步骤.
- 打开命令行窗口进入到 Python2 的运行环境, 运行命令 pip show 2to3, 如果显示如下的信息, 说明还没有安装 2to3 包.
(python-2-7-18) songs-MacBook-Pro:~ songzhao$ pip show 2to3 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support WARNING: Package(s) not found: 2to3
- 在命令行中运行以下命令:`pip install 2to3` 来安装 Python 2to3 工具.
(python-2-7-18) songs-MacBook-Pro:~ songzhao$ pip install 2to3 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Collecting 2cto3 Downloading https://files.pythonhosted.org/packages/70/06/9bd891a16b16572e42d124627048a673f9e201c490ef1638b689a94cacd7/2to3-1.0.tar.gz Building wheels for collected packages: 2to3 Building wheel for 2to3 (setup.py) ... done Created wheel for 2to3: filename=2to3-1.0-cp27-none-any.whl size=1665 sha256=ffdb8ba84dfdea416d07cd714657f5077387e2587c99d22a0513eecf9c64d932 Stored in directory: /Users/songzhao/Library/Caches/pip/wheels/b7/d3/5f/c8c64fb009f1c7ef72d2533821dbf329a728400ccb463a41c6 Successfully built 2to3 Installing collected packages: 2to3 Successfully installed 2to3-1.0
- 再次运行命令 pip show 2to3, 如果看到如下的信息, 说明已经成功的安装了 Python 2to3 工具.
(python-2-7-18) songs-MacBook-Pro:~ songzhao$ pip show 2to3 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Name: 2to3 Version: 1.0 Summary: Adds the 2to3 command directly to entry_points. Home-page: UNKNOWN Author: xoviat Author-email: [email protected] License: MIT Location: /Users/songzhao/anaconda3/envs/python-2-7-18/lib/python2.7/site-packages Requires: Required-by:
- 在命令行中切换到包含Python 2.x代码的目录,并运行以下命令:`2to3 -W -n -o converted_code/ original_code/`, 这将在名为`converted_code`的目录中生成转换后的Python 3.x代码。
- 标志“-W”或“–write-unchanged-files”指示 2to3 工具生成并且写入输出文件,而不管源文件是否需要进行任何更改。
- 将源文件用作备份文件时需要-n标志。如果不覆盖源文件,则没有意义。
- -o或–output-dir选项用来设置已处理的输出文件的保存目录。
2. 使用 2to3 工具将 Python2 源代码转换成 Python3 源代码示例.
- 下面是 Python2.7 源代码. 我们在 Python2.7 环境中可以一行一行的运行如下的代码.
# Python 2.x示例代码 print "Hello, World!" name = raw_input("Input your name: ") print "You are welcome ", name x = input("Enter a number: ") print "The square of", x, "is", x ** 2
- 将以上代码保存在当前目录下的 python2code.py 文件中.
- 在命令行窗口运行命令 2to3 -W -n -o ./converted-code/ ./python2code.py.
(python-2-7-18) songs-MacBook-Pro:python-2to3-convertion songzhao$ 2to3 -W -n -o ./converted-code/ ./python2code.py WARNING: --write-unchanged-files/-W implies -w. lib2to3.main: Output in './converted-code/' will mirror the input directory '.' layout. RefactoringTool: Skipping optional fixer: buffer RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: set_literal RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: Refactored ./python2code.py --- ./python2code.py (original) +++ ./python2code.py (refactored) @@ -1,10 +1,10 @@ # Python 2.x示例代码 -print "Hello, World!" +print("Hello, World!") -name = raw_input("Input your name: ") +name = input("Input your name: ") -print "You are welcome ", name +print("You are welcome ", name) -x = input("Enter a number: ") +x = eval(input("Enter a number: ")) -print "The square of", x, "is", x ** 2 +print("The square of", x, "is", x ** 2) RefactoringTool: Writing converted ./python2code.py to ./converted-code/python2code.py. RefactoringTool: Files that were modified: RefactoringTool: ./python2code.py
- 上面的命令运行完成后,会在当前目录下的 converted-code 子目录下生成一个转换后的 Python3 源代码的文件。
- 以下是的Python3源代码文件的内容。
# 转换后的Python 3.x示例代码 print("Hello, World!") name = input("Input your name: ") print("You are welcome ", name) x = eval(input("Enter a number: ")) print("The square of", x, "is", x ** 2)
3. 总结:
- 选择使用2to3工具自动将Python 2.x代码转换为Python 3.x代码非常简单。然而,自动化工具并非完美,转换后的代码可能需要进一步手动修改和调整,以确保其在Python 3.x环境中正确运行。因此,建议在转换完成后进行充分的测试和调试。
- 将Python 2.x代码转换为Python 3.x代码是一个重要的迁移过程,但通过使用这些自动化工具,您可以大大减轻工作量,并更快地实现迁移。同时,这也是一个学习新功能和语法的好机会,因为Python 3.x版本引入了许多改进和新特性。希望本篇博客对您在Python代码迁移过程中有所帮助!