User guide for converting Python2 code to Python3 code tool under Linux system and Windows system

Introduction

This article mainly introduces the Python2 code conversion to Python3 code tool 2to3.py or 2to3 command usage guide under Linux system and Windows system.

Project Scenario and Problem Description

The last version of Python2 is 2.7, which will stop supporting completely in 2020. Some environments are inconvenient to install Python2 and Python3 at the same time, or in the environment of using Python3, you do not want to install Python2, but some project codes are based on Python2, and you want to convert the unique code part of Python2 into Python3 executable code , but the workload is relatively large. The following describes how to use the scripts 2to3.pyor instructions that come with the Python3 standard library.2to3


Windows environment uses 2to3.py

2to3.pyThis script is usually installed together with the Python interpreter in the Tools/scripts directory of the current environment's Python path. (eg: D:\Anaconda\anaconda3\Tools\scripts)
2to3.py file location
Take the code VeraCode.py, Aaa.py, Bbb.py, Ccc.py written based on Python2 in the E:\workspace\pycharm\Vera folder as an example.

1. To convert a single Python2 code into Python3 code available instructions:

python 2to3.py E:\workspace\pycharm\Vera\VeraCode.py

After the instruction is executed, the Python2 syntax in VeraCode.py will be modified to Python3 code.

2. Convert a single Python2 code to Python3 code and back up the original code Available instructions:

python 2to3.py -w E:\workspace\pycharm\Vera\VeraCode.py

After the instruction is executed, the syntax of Python2 in VeraCode.py will be modified to the code of Python3, and the original code is backed up as VeraCode.py.bak.
Among them, the parameter -wis to back up the original code, which can be seen from the following 2to3.py code, and there are other parameters that can be checked by yourself

parser.add_option("-w", "--write", action="store_true",
                      help="Write back modified files")

3. Convert all Python2 codes in the entire folder to Python3 codes and back up the original code Available instructions:

python 2to3.py -w E:\workspace\pycharm\Vera

If there are many Python files in the project directory, there is no need to back them up, and no parameters can be used -w.


The Linux environment uses the 2to3 instruction

In fact, there is also a 2to3.py script similar to Windows in the directory where Python is installed in the Linux environment. The location is in /usr/lib/python3.10/lib2to3folder, main.py function.
But the Linux environment can directly use 2to3the command.

First, you can use the following command to check whether it has been installed 2to3:

find /usr/local/ -name "2to3"

If not, you can install it with pip:

pip install 2to3

After installation, check whether the installation is complete, and you can see find /usr/local/ -name "2to3"the execution results of the command as follows:

/usr/local/bin/2to3

Next, the method of use is similar to the Windows system.
Take the code VeraCode.py, Aaa.py, Bbb.py, and Ccc.py written based on Python2 in the /app/models/Vera folder as an example.
1. To convert a single Python2 code into Python3 code available instructions:

2to3 /app/models/Vera/VeraCode.py

After the instruction is executed, the Python2 syntax in VeraCode.py will be modified to Python3 code.

2. Convert a single Python2 code to Python3 code and back up the original code Available instructions:

2to3 -w /app/models/Vera/VeraCode.py

After the instruction is executed, the syntax of Python2 in VeraCode.py will be modified to the code of Python3, and the original code is backed up as VeraCode.py.bak.

3. Convert all Python2 codes in the entire folder to Python3 codes and back up the original code Available instructions:

2to3 -w /app/models/Vera

After the instruction is executed, you can see the modified part of the code. Take a chestnut:
Code conversion example


Reference blog:

Convert python2 code to python3
python2 to python3 code

Guess you like

Origin blog.csdn.net/qq_39691492/article/details/131187314