Why should you migrate to Python3+?
I would like to remind you that the support for Python 2.7 will have been ended by 2020. The funniest thing which I have discovered is a countdown counter https://pythonclock.org/, so you can check how many days are left. I hope this clock scared you a little and you will make first step to create an issue in your backlog with title ‘Porting to Python3’. I know that tasks are unwelcome for management team, but maybe you are able to divide it into small chunks and provide it in every release small changes which will support Python 3. A lot of functionalities have backward compatibility. It is a good reason to implement it in current tasks.
Don’t leave migration process at the last-minute!
How can you start porting process?
Basics steps:
- Test your app
- Measure test coverage
- Check differences
- Update code
Test your app
Of course you need tests. If you don’t have any tests you have to add it, because the porting process will be more easier. The more tests are present, the less unexpected bugs happen. I use pytest to test application. Tests are very small and clear, so it is the reason why I use it.
Measure test coverage
The higher percentage of test coverage the better code(Don’t look for edge cases, it won’t work. Focus on test quality not only on tests quantity). The test coverage you can measure via coverage package. You can install it via:
pip install coverage
Check differences
Code examples:
Python 2 vs Python 3
'{}'.format(b'text') '{}'.format(b'text'.decode())
print 2 print(2)
unicode('text') str('text')
{}.items() list({}.items())
{}.keys() list({}.keys())
filter(function, seq) list(filter(function, seq))
map(function, seq) list(map(function, seq))
The above code samples shows that a lot of code implementations were changed in Python3. For example filter or map functions returns an iterator not a list. More code examples you can find in site divide into python3.
Update code
You can update your project manually, but I can really recommend a 2to3 package to porting from Python2 to Python3+. It saves your time, probably it won’t solve all cases correctly, but the work will be done much quicker.