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

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

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++.

Back to blog

Leave a comment

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