I'm writing cross-compatible Python 2 and 3 code with some help from this cheatsheet. I've noticed there are different packages and modules that help to do this: the future
package (e.g. future.utils
etc), the six
package, and the built in __future__
module.
Are there any differences to be aware of when using these packages? Should I be mixing and matching them, or is it possible to write fully cross-compatible code be written with just one of them?
The latest stable version is Python 3.9 which was released in 2020. The nature of python 3 is that the changes made in python 3 make it incompatible with python 2. So it is backward incompatible and code written in python 3 will not work on python 2 without modifications.
Python 3 has an easier syntax compared to Python 2. A lot of libraries of Python 2 are not forward compatible. A lot of libraries are created in Python 3 to be strictly used with Python 3. Python 2 is no longer in use since 2020.
# Python 2 and 3: forward-compatible from builtins import range for i in range(10**8): ... # Python 2 and 3: backward-compatible from past.
Just open the python files in the pycharm editor, it will show warnings if the code is not compatible to Python2 or Python3. Here is the screenshot where it shows print command syntax warning. Minimum Individual cost is $89/year. Is this still erroneous in today's Python 3?
In terms of python 2-3 compatibility:
__future__
- is a built-in module in python which allows you to use optional features in python versions where they are optional (vs mandatory). For example, unicode_literals
was optional in python2.7
but became mandatory in python3.0
.
six
- mostly renames modules/functions to produce higher compatibility between python2
to python3
, but doesn't actually backport (or forward-port) functionality. It is also supported for python versions >=2.4
.
future
- more modern, only supports python>=2.6,>=3.3
, and is more rich in features.
Seems like there is some agreement that future
is preferred to six
if you can drop support for old versions of python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With