Convert Python2 to 3 With Backwards Compatibility Using Future

Python 3 has been out for nearly 15 years now and is currently the standard, with the most recent version being 3.10 https://www.python.org/downloads/. However, some legacy projects remain in Python 2 despite it being deprecated. This typically occurs in large projects with multiple dependencies where making the switch can be dangerous if not taken with precaution. Changing to Python 3 in one dependency can hinder another dependency that is still in Python 2 that relies on the latter dependency, which can lead to issues in your production pipelines. The Futurize library in Python thankfully allows us to convert code while making it backward compatible with Python 2 to avoid issues like this. I will be showing how to futurize a piece of code with the following code sample:

Now the above code works just fine in Python 2, but if we try to run this with Python 3 then we are hit with the following error:

SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello world")?

However taking the steps to futurize this code will allow us to run it in Python 2 and 3, which is great in cases where backward compatibility is needed.

Step 1-) Install the future package.

   > pip install future 

Step 2-) Convert the script with stage 1 futurization.

To do this, type the following into the command line:
   > futurize --stage1 -w <path_to_file>

Which will cause the file to change to:

Note that this file is still not runnable in Python 3, which is usually the case after applying stage 1. The goal of stage 1 is to create the diff for the entire porting process without introducing bugs into your program. However, if your file runs fine in Python 2 and 3 after running only stage 1, you do not need to proceed to stage 2 (step 3 in this tutorial can be skipped) Also note that you can choose to not convert the file by omitting the -w flag, doing so will only show you the potential differences of the switch. 

Step 3-) Run stage 2 futurization.

 To do this, type the following into the command line:
   > futurize --stage2 -w <path_to_file>

Which will cause the file to change to:

Running stage 2 adds the dependency to the future package, the goal of stage 2 is to make more safe changes to the code. This file is now runnable in Python 3 with the following output: 

Hello world
1
2
name 'trying_to_check_error' is not defined Error Caused

Step 4-) Post futurization

Now after this process is done it is highly recommended you test the code in Python 2 and 3 to see if they have the same results, if your application is dealing with a lot of string and binary data you may run into a significant amount of encoding issues. Sadly packages like these cannot convert the semantic aspect of the code, so if you are dealing with issues in that aspect of the conversion you must resort to manual refactoring.

Conclusion:

Futurize is a convenient way to have reliable Python 2/3 compatible code. It is fitting for legacy systems that need this backward compatibility. If it is possible to decouple from Python 2 completely you should proceed with doing so, since Python 3 is fully supported and has numerous benefits over Python 2, especially in the performance aspect. Hope this tutorial helped and your code is 2/3 compatible. If you have any questions please comment down below or reach out to me personally. Thanks for reading!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Implementing Google reCAPTCHA in a Simple React and Node.js App

Implementing Google reCAPTCHA in a Simple React and Node.js App

Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

Raspberry Pi Pico/Pico W Free Simulator

Raspberry Pi Pico/Pico W Free Simulator

Discover how to simulate Raspberry Pi Pico projects using Wokwi, a free online simulator for Arduino and MicroPython....

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interface with the MPU6050 using the Raspberry Pi Pico W in C++.

How to Write your First C++ Program on the Raspberry Pi Pico W

How to Write your First C++ Program on the Raspberry Pi Pico W

Write your first C++ Program on the Pico W in a few simple steps.

How to Use ThingSpeak with the Raspberry Pi Pico W

How to Use ThingSpeak with the Raspberry Pi Pico W

Learn how to create a real-time environmental monitoring system with the Raspberry Pi Pico W and ThingSpeak!

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.