Porting Python 2.7 Code to Python 3.X
Full-Stack developer, software architect & IT consultant at Jellyfish.tech
Updating Python 2 to Python 3 | Painless Migration Guide
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.
Obviously, some software written in Python 2 should be migrated to Python 3.
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.
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.
Reasons for Code Porting
- Large community support.
- 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:
- native support for asynchronous I/O (asyncio);
- new MRO;
- f strings syntax;
- new packaging;
- assignment expressions;
- type annotations;
How to Port Python 2 to Python 3
The biggest challenge impeding from 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.
What should be done when upgrading Python version:
1. 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.
2. Understand the key difference between Python 2 and 3.
3. 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.
4. 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.
5. 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.
6. Start using Python 3 in production.
7. Remove Python 2 compatibility code.
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.
Previously published at https://jellyfish.tech/updating-python-2-to-python-3/
Subscribe to get your daily round-up of top tech stories!