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!

Back to blog

Leave a comment

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