Porting Python 2.7 Code to Python 3.X
In December 2008, Python software foundation released version 3.0 introducing big changes to improve Python. As many people after the release did not want to upgrade their projects, the developers had been supporting both versions for some time not to hurt users’ feelings.
Although much time is given for code porting, many haven’t done it yet. Meanwhile, supporting two versions consumes much time & effort, preventing the developers from further Python 3.0 improvement.
That’s why the Python foundation decided to announce:
Users are urged to migrate to a supported version of Python 3 in order to benefit from its many improvements, as well as to avoid potential security vulnerabilities in Python 2.x after April 2020.
The biggest challenge impeding from almost effortless migration to Python 3.0 is a backward incompatibility with version 2.0. It means that the code Python 2.x will always return errors while running on Python 3.0. The dynamic Python standardization makes it difficult to mechanically migrate from Python 2 to Python 3.
This is where the help of the professional Python developers & software architects could be required.
- Difference between Python 2 and Python 3
- Reasons for Code Porting
- How to Port Python 2 to Python 3
- Examples of Large Companies
According to the Google Trends data, the people’s interest in Python 3.0 has been continuously growing to surpass its earlier version:
But what features did the community add to Python 3.0 to make such a big difference?
|Python 2||Python 3|
|error handling||except NameError, err||except NameError as err|
|print function is treated more as a statement||print() function, treated as a function|
|integer division||numbers without any digits are integers (3/2 = 1)||intuitive way of treating numbers: 3/2 = 1.5|
|integer size||limited to 32 bits||unlimited|
|range||xrange method||range method|
|rules of ordering comparisons||complex||simplified|
|leak of variables||never changes||changes while using it inside for-loop|
A few best new features in Python 3:
- native support for asynchronous I/O (asyncio);
- new MRO;
- f strings syntax;
- new packaging;
- assignment expressions;
- type annotations;
and many other awesome things!
def main(): print "Hello World!" if __name__== "__main__": main()
def main(): print("Hello World!") if __name__== "__main__": main()
Who should consider the migration?
Application owners with a clear vision of future product development.
The majority of applications with a reasonable projection for the future have been already ported to Python 3.X. If you’re planning to scale your app or add new features over time, you should definitely choose the Python 3.X language versions, as unsupported code could lead to serious security gaps.
Here are more reasons why Python 3 is worth being migrated to:
- Large community support
So it’s much quicker & easier to fix code bugs or get help from the community.
- Standard library improvements are available by default.
- Simplified process of detecting and fixing the bugs.
- Support of modern technologies: AI, big data, machine learning.
What will happen if I do not port the code?
If you or your vendor find a security problem in Python 2, nobody from the volunteers will help fix it, as this version is no longer supported. Except for the security issues, you won’t be able to use a set of good packages built for Python 3.
However, not every Python application should be upgraded. As for closed and self-sufficient systems without a scaling up intention, porting the code could have rather negative than the positive financial outcome, as you’ll spend the resources on doing the job that won’t be of much benefit.
What should be done when upgrading Python version:
- Choose the strategy. Depending on your objectives, your vendor can help you pick up a custom strategy fitting for your project. Among the most common ones, it could be code rewriting, ending up with Python 2.0 version support and supporting Python 3.0 instead, or porting the project to Python 3.0 without adding new Python 2.0 features.
- Understand the key difference between Python 2 and 3. You can use the above-mentioned table or go to one of the posts highlighting the difference. Being aware of the core changes in version 3.0 will tell you the areas to focus on.
- Audit the code. Any upgrade, modernization, or improvement is impossible without the code audit, as it helps the developers define the possible problems, bottlenecks, and re-build the system’s architecture to enhance the performance if required.
- Ensure good test coverage. In a perfect world, every major step of code porting should be accompanied by unit tests to ensure the code complies with all its functions. A good test coverage allows detecting the problems directly on the stage of development that makes it easier to fix the bugs.
- Use CI/CD concepts. Iterate releases ensured by the concepts of continuous integration & delivery help deliver a better code making the whole application work better.
- Start using Python 3 in production.
- Remove Python 2 compatibility code.
Want to learn more about updating your project? Get a free consultation:
If your digital product is aimed at advancing with the times, code porting is a mandatory option without which the further development of your project is under the question.
May the examples of the applications-giants dispel any of your doubts:
The large-scale application with more than 1 billion users a month has gone through the upgrade. It takes 10 months for the technical team to port the app from Python 2.7 to Python 3, starting from removing the third-party packages that Python 3 doesn’t support, and unit testing to the smooth product rollout.
In 2011, Jason Fried and a couple of Facebook engineers were struggling to make Python 3 the dominant version at Facebook. Although, it “was going to take some time and much diplomacy”, Fried and his team reached the result. For now, it’s embarrassing at Facebook to run only Python 2 code.
Watch his YouTube video to get the details:
The set of actions to port Python code from 2.0 to 3.0 is similar to the process of legacy modernization. The development team should proceed with caution according to a comprehensive plan and estimate the risks properly.
Porting the Python code is still a challenge impossible without a dedicated team of developers but the outcome is worth it in most cases.
Considering Python migration? It’s better to check with a technical partner having similar cases to decide on the value it brings (or doesn’t bring) to your business before taking steps towards updating your version of Python.