Eight tips for making python compatible with both python2 and python3

  • 2020-04-02 13:50:52
  • OfStack

Someone on the python mailing list said "python3 won't be universal in 10 years." It seems to me a bit pessimistic that python3 and python2 are incompatible, but they're not as different as many people think. You can support both python2 and python3 very well with only a few changes to your own code. I'm going to give you a brief overview of how to get your python code to support both python2 and python3.

1. Discard python versions prior to python 2.6

Previous versions of python prior to python 2.6 lacked some new features that made your migration work a lot harder. Drop support for previous versions if you don't have to.

Use the 2to3 tool to check the code

2to3 is a code conversion tool that automatically converts code from python2 to python3. Of course, unfortunately, the converted code doesn't do anything about python2 compatibility. So we don't really use the code that 2to3 converts. Execute 2to3 t.p to see the output and fix the problem.

Use python-3 to execute python programs

2to3 can detect many compatibility problems with python2&3, but there are also many problems that 2to3 cannot detect. When the -3 parameter is added, the program will run at a time when python2 and python3 are inconsistent at the console, and problems that 2to3 cannot handle are indicated. Python3 and python2, for example, have changed the rules for division. Executing 4/2 with the -3 parameter prompts DeprecationWarning: classic int division.

S: from arbitration/arbitration/arbitration/arbitration/arbitration/arbitration/arbitration

Use the future features of python after "from successively future__ import". The full future feature of python is available with s/s. All characters in python3 become unicode. In python2 unicode characters are defined with u in front of them, but in 3 they are not, and the program will not compile after u. To solve this problem, "from future import unicode_literals", so that the behavior of characters in python2 will be consistent with that in python3, and normal characters defined in python2 will be automatically recognized as unicode.

V. import

Python3 is "missing" a lot of python2 packages, which in most cases are simply renamed. We can handle these problems when we import.

try:#python2
    from UserDict import UserDict
    # Suggestions in accordance with the python3 Conduct by name import
    from UserDict import DictMixin as MutableMapping
except ImportError:#python3
    from collections import UserDict
    from collections import MutableMapping

Write programs using python3

Print in python2 is the keyword, and in python3 print becomes the function. In fact, in python2.6 you already have the print function, so you can just write print as shown in 2to3. Python3 makes some changes to the handling of the exception, which is similar to print, and simply follows the instructions in 2to3.

Check the currently running version of python

Sometimes you might have to write different code for python2 and python3, and you can check the python version of the current system with the following code.

import sys
if sys.version > '3':
    PY3 = True
else:
    PY3 = False

Eight, six

Six provides some simple tools to encapsulate the differences between Python 2 and Python 3. I don't really recommend using six. If you don't need to support python versions prior to python2.6, it's easier to handle compatibility without using six. Using six makes your code more like python2 than python3.
The popularity of python3 requires a push from every single python. You may not be able to upgrade to python3 immediately, but start writing python3-compatible code now and upgrade to python3 when conditions are right.

Note: differences between python2 and python3

If you have a more complete understanding of the problems associated with migrating from Python 2 to Python 3, we recommend Porting to Python 3. This is a free Python book.


Related articles: