How To Automatically Convert Python 2.X Code To Python 3.X Code

With the release of Python 3. x and the discontinuation of support for Python 2. x, many developers face an important task: converting their Python 2. x code into Python 3. x code. Although this task may seem cumbersome and time-consuming, fortunately, there are some automated tools that can help us complete this task. In this blog post, I will introduce several commonly used methods to help you automatically convert Python 2. x code to Python 3. x code.

1. Use Python 2to3 Library Tool.

Python officially provides a tool called 2to3, specifically designed to convert Python 2. x code into Python 3. x code. The Python 2to3 tool can automatically detect and convert incompatible syntax and function calls in source code. The following are simple steps to use the 2to3 tool.

Open a command line window and enter the Python 2 runtime environment. Run the command pip show 2to3. If the following message is displayed, it indicates that the 2to3 package has not been installed yet.

(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

Run the following command from the command line: ‘pip install 2to3‘ to install the Python 2to3 tool.

(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

Run the command pip show 2to3 again. If you see the following message, it indicates that the Python 2to3 tool has been successfully installed.

(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: xoviat@gmail.com
License: MIT
Location: /Users/songzhao/anaconda3/envs/python-2-7-18/lib/python2.7/site-packages
Requires: 
Required-by:

Switch to the directory containing the Python 2. x code files on the command line and run the following command: 2to3 -W -n -o converted_ code/ original_code/.

The above command will convert all the source python files in original_code folder to python3.x format and save the converted files in the folder converted_code.

The flag -W or –write-unchanged-files indicates that the 2to3 tool generates and writes output files, regardless of whether any changes are needed to the source file.

The flag -n means no backups, it will not write backups for modified files.

The flag -o or –output-dir is used to specify the folder where the converted output python3.x files are saved in.

The -n and -o flags must be used together at the same time, otherwise it will show errors like below.

(python-2-7-18) songs-MacBook-Pro:python-2to3-convertion songzhao$ 2to3 -W -o ./converted-code/ ./pythoncode.py
WARNING: --write-unchanged-files/-W implies -w.
Usage: 2to3 [options] file|dir ...

2to3: error: Can't use --output-dir/-o without -n.

You can run the command 2to3 –help to list all the flags’ meanings.

(python-2-7-18) songs-MacBook-Pro:python-2to3-convertion songzhao$ 2to3 --help
Usage: 2to3 [options] file|dir ...

Options:
  -h, --help            show this help message and exit
  -d, --doctests_only   Fix up doctests only
  -f FIX, --fix=FIX     Each FIX specifies a transformation; default: all
  -j PROCESSES, --processes=PROCESSES
                        Run 2to3 concurrently
  -x NOFIX, --nofix=NOFIX
                        Prevent a transformation from being run
  -l, --list-fixes      List available transformations
  -p, --print-function  Modify the grammar so that print() is a function
  -v, --verbose         More verbose logging
  --no-diffs            Don't show diffs of the refactoring
  -w, --write           Write back modified files
  -n, --nobackups       Don't write backups for modified files
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
                        Put output files in this directory instead of
                        overwriting the input files.  Requires -n.
  -W, --write-unchanged-files
                        Also write files even if no changes were required
                        (useful with --output-dir); implies -w.
  --add-suffix=ADD_SUFFIX
                        Append this string to all output filenames. Requires
                        -n if non-empty.  ex: --add-suffix='3' will generate
                        .py3 files.

2. Example Of Converting Python 2 Source Code To Python 3 Source Code Using The 2to3 Tool.

The following is the Python 2.7 source code We can run the following code line by line in the Python 2.7 environment.

# Python 2.x example source code.
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

Save the above code in the pythoncode.py file in the current directory.

Run the command 2to3 -W -n -o ./converted-code/ ./pythoncode.py in the command line console window.

(python-2-7-18) songs-MacBook-Pro:python-2to3-convertion songzhao$ 2to3 -W -n -o ./converted-code/ ./pythoncode.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 ./pythoncode.py
--- ./pythoncode.py	(original)
+++ ./pythoncode.py	(refactored)
@@ -1,10 +1,10 @@
 # Python 2.x example source code.
-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 ./pythoncode.py to ./converted-code/pythoncode.py.
RefactoringTool: Files that were modified:
RefactoringTool: ./pythoncode.py

After the above command is completed, a converted Python 3 source code file will be generated in the converted-code subdirectory of the current directory。

The following is the content of the automatically converted Python 3 source code file.

# Converted Python 3. x sample code
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. Summary.

Choosing to use the 2To3 tool to automatically convert Python 2. x code to Python 3. x code is very simple. However, automation tools are not perfect, and the converted code may require further manual modifications and adjustments to ensure it runs correctly in the Python 3. x environment. Therefore, it is recommended to conduct sufficient testing and debugging after the conversion is completed.

Converting Python 2. x code to Python 3. x code is an important migration process, but by using these automated tools, you can greatly reduce the workload and achieve faster migration. At the same time, this is also a great opportunity to learn new features and syntax, as Python 3. x introduces many improvements and new features. I hope this blog is helpful for you in the Python code migration process.

4. Video Demo For This Article.

You can watch the video of this example article below.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.