You only need to make slight modifications to your code to support both python2 and python3. Below I will briefly introduce how to make your python code support both python2 and python3.
Abandon python versions before python 2.6
Python versions before python 2.6 lack some new features, which will bring a lot of trouble to your migration work. If you don't have to, give up support for previous versions.
Use the 2to3 tool to check the code
2to3 is a code conversion tool that comes with python, which can automatically convert python2 code into python3 code. Of course, unfortunately, the converted code does not do anything for python2 compatibility. So we don't really use the 2to3 converted code. Execute 2to3 t.py to view the output information and correct related problems.
Use python -3 to execute python programs
2to3 can check out many python2&3 compatibility issues, but there are also many issues that 2to3 cannot find. After adding the -3 parameter, the program will prompt on the console that python2 and python3 are inconsistent, and that 2to3 cannot handle it. For example, the division processing rules have been changed in python3 and python2. Executing 4/2 with the -3 parameter will prompt DeprecationWarning: classic int division .
from __future__ import
"from __future__ import" can be used to use the future features of python. Python's complete future features can be found in __future__ . All characters in python3 have become unicode. In python2, unicode characters need to be added with u in front of the character when defining, but in python 3, u is not required, and the program will not be compiled after adding u. In order to solve this problem, you can "from future import unicode_literals", so that the behavior of characters in python2 will be consistent with that in python3, and ordinary characters defined in python2 will be automatically recognized as unicode.
Import problem
There are "missing" many python2 packages in python3. In most cases, the names of these packages have just been changed. We can deal with these problems when importing.
try:#python2
from UserDict import UserDict
#It is recommended to import according to the name of python3
from UserDict import DictMixin as MutableMapping
except ImportError: #python3
from collections import UserDict
from collections import MutableMapping
Use python3 to write programs
In python2, print is a keyword, but in python3, print becomes a function. In fact, the print function is already included in python2.6, so for print, you can just follow the tips given in 2to3 and change it to the new way of writing. Some changes have been made to exception handling in python3. This is similar to print. You can modify it directly according to the prompts in 2to3.
Check the currently running python version
Sometimes you may have to write different codes for python2 and python3, you can use the following code to check the python version of the current system.
import sys
if sys.version > '3':
PY3 = True
else:
PY3 = False
six
six provides some simple tools to package between Python 2 and Python 3 differences between. I don't really recommend using six. If you don't need to support python versions before python2.6, it is easier to deal with compatibility issues even without six. Using six will make your code more like python2 than python3.
The popularity of python3 needs the promotion of every pythoner. Maybe you can’t upgrade to python3 immediately, but please start writing python3-compatible code now and upgrade to python3 when conditions are ripe.