As an Amazon Associate I earn from qualifying purchases.
So you have legacy Python 2 code and you need to switch to Python 3. But how do you do this? This tutorial will cover the differences between python 2 and python 3 as well as how to painlessly upgrade from Python 2 to Python 3 using the tool 2to3
.
The short answer
To upgrade code from Python 2 to Python 3 run the following:
1. Download the latest version of Python
2. Install 2to3 using pip ‘pip install 2to3‘
3. Run the following on your python file ‘2to3 -w python_file.py‘
Key differences
Support Python 2 is not supported as of January 2020 | Support Python 3 is still supported today |
Print function The print function in Python 2 was a special statement that followed the following syntax: print “Hello World!” | Print function In Python 3, print is an actual function and has slightly different syntax print(“Hello World!”) |
Division Python 2 rounds down numbers to the nearest integer. e.g: 5 / 2 = 2 | Division Python 3 will give you the expected result 5 / 2 = 2.5 |
Encoding ASCII was used in Python 2 to store strings. | Encoding Unicode is used now to store strings in Python 3 |
Libraries Many libraries are not forwards compatible with Python 3 | Libraries Most newer libraries are built for Python 3 are not supported by Python 2 |
Upgrading
Now that you know the key differences between Python 2 and Python 3, let’s have a look at how we can painlessly upgrade. If you haven’t already done so go ahead and install the latest version of Python. Once this is done, run the following command with pip
:
pip install 2to3
Note: if you are having trouble with pip
check out this tutorial What is Python Pip? – How to get started
Before upgrading, it is highly recommended that you create a backup of your entire Python project. Failure to do so can result in lost/corrupt code or other undesirable outcomes.
Once you have made a backup, navigate to your python project directory.
Upgrading one Python file
To see the result of upgrading one python file but not write the file, run the following command:
2to3 your_python_file.py
Once you do this, you will see everything that will be changed. If you are happy with these changes run the same command with a -w
flag to write the changes:
2to3 -w your_python_file.py
Example
I have the following Python 2 code:
x = 5 / 2
print "x=",x
When I run this code, I get the following output:
$ python python2code.py
x= 2
When I run 2to3
, it will show me that the print statement on the second line will be updated:
$ 2to3 python2code.py
RefactoringTool: Refactored python2code.py
--- python2code.py (original)
+++ python2code.py (refactored)
@@ -1,2 +1,2 @@
x = 5 / 2
-print "x=",x
+print("x=",x)
RefactoringTool: Files that need to be modified:
RefactoringTool: python2code.py
Since I am happy with these changes, I will rerun the 2to3 command with the -w flag:
$ 2to3 -w python2code.py
RefactoringTool: Refactored python2code.py
--- python2code.py (original)
+++ python2code.py (refactored)
@@ -1,2 +1,2 @@
x = 5 / 2
-print "x=",x
+print("x=",x)
RefactoringTool: Files that were modified:
RefactoringTool: python2code.py
As you can see above the file has been modified. When I run the modified file in Python 3 I get the following output:
$ python3 python2code.py
x= 2.5
As you can see, the result of x is different because we are using Python 3 now.
Upgrading entire Python project:
To upgrade your python project, run 2to3
and specify the argument to 2to3
as the project folder.
2to3 your_python_project_folder
Once ran, you will be able to see all the changes 2to3 will make to your project. If you are happy with these changes rerun 2to3
and specify the -w
flag to write the files:
2to3 -w your_python_project_folder
Example
I have a project named python2code
and I want to update it to Python 3. To upgrade, I will start by running 2to3
passing python2code
as the argument:
$ 2to3 python2code
RefactoringTool: Refactored python2code/python2code.py
--- python2code/python2code.py (original)
+++ python2code/python2code.py (refactored)
@@ -1,2 +1,2 @@
x = 5 / 2
-print "x=",x
+print("x=",x)
RefactoringTool: Refactored python2code/python2code_2.py
--- python2code/python2code_2.py (original)
+++ python2code/python2code_2.py (refactored)
@@ -1 +1 @@
-print "Hello World"
+print("Hello World")
RefactoringTool: Files that need to be modified:
RefactoringTool: python2code/python2code.py
RefactoringTool: python2code/python2code_2.py
As you can see, 2to3
recursively went through all the files and outputted the updates to each file that it will before. Again, this has not been written yet. To write the file, I will rerun the same command but using the -w
flag:
$ 2to3 -w python2code
RefactoringTool: Refactored python2code/python2code.py
--- python2code/python2code.py (original)
+++ python2code/python2code.py (refactored)
@@ -1,2 +1,2 @@
x = 5 / 2
-print "x=",x
+print("x=",x)
RefactoringTool: Refactored python2code/python2code_2.py
--- python2code/python2code_2.py (original)
+++ python2code/python2code_2.py (refactored)
@@ -1 +1 @@
-print "Hello World"
+print("Hello World")
RefactoringTool: Files that were modified:
RefactoringTool: python2code/python2code.py
RefactoringTool: python2code/python2code_2.py
As you can see my entire python project was upgraded from Python 2 to Python 3.
If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.
Interested in creating your own algo trading bot? Check out my free series on algo trading here: https://www.conorjohanlon.com/category/algotrader/
As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.