8 tips to make Python compatible with both Python2 and Python3

Source: Internet
Author: User
The Python mailing list says that "Python3 cannot be popularized within 10." In my opinion this kind of view is somewhat too pessimistic, python3 and python2 are incompatible, but the difference between them is not as large as many people think. You just need to make some changes to your code to support both Python2 and Python3. I'll briefly explain how to get my Python code to support Python2 and Python3 at the same time.

First, discard Python version prior to Python 2.6

Python versions prior to Python 2.6 lack some new features that can cause a lot of trouble with your migration effort. If not, give up support for the previous version.

Second, use the 2to3 tool to check the code

2to3 is a python-brought code conversion tool that automatically converts python2 code into Python3 code. Of course, unfortunately, the converted code does not do any processing for python2 compatibility. So we don't really use 2TO3 to convert the code. Perform 2to3 t.py view the output information and fix the related issues.

Third, use Python-3 to execute Python program

2to3 can check out a lot of python2&3 compatibility issues, but there are a lot of problems that 2to3 can't find. After adding the-3 parameter, the program will run with Python2 and Python3 inconsistent on the console, and 2to3 the inability to handle the problem prompt. For example, Python3 and python2 have changed the processing rules of division. Using the-3 parameter to execute 4/2 will prompt deprecationwarning:classic int division.

Iv. from __future__ Import

"From __future__ import" will make use of Python's future features. Python's full future feature is visible __future__. All the characters in the Python3 become Unicode. Unicode characters in Python2 need to precede characters with u in the definition, but home U is not required in 3, and the program will not compile and pass after you add U. In order to resolve this issue you can "from the future import Unicode_literals", so that the behavior of Python2 characters will be consistent with Python3, the definition of ordinary characters in Python2 is automatically recognized as Unicode.

V. Import issues

Python3 "Less" a lot of python2 bags, in most cases these packages are changed a name. We can deal with these issues at import time.
Copy the Code code as follows:

Try: #python2
From userdict import userdict
#建议按照python3的名字进行import
From userdict import dictmixin as Mutablemapping
Except Importerror: #python3
From collections Import UserDict
From collections Import mutablemapping

Vi. using Python3 to write programs

Print is the keyword in python2, and print becomes a function in Python3. As a matter of fact, the print function is already in python2.6, so you can change it to print directly by following the hints given in 2to3. In the Python3 to the exception of the processing made some changes, this and print similar, directly follow the instructions in the 2to3 to modify.

Vii. checking the currently running Python version

Sometimes you may have to write different codes for Python2 and Python3, and you can check the Python version of the current system with the following code.
Copy the Code code as follows:

Import Sys
If Sys.version > ' 3 ':
PY3 = True
Else
PY3 = False

Eight, six

Six provides a few simple tools to encapsulate the differences between Python 2 and Python 3. I don't recommend using six. If you don't need to support the Python version before python2.6, it's easier to deal with compatibility issues without six. Using six will make your code more like Python2 than Python3.
The popularity of Python3 needs to be driven by every pythoner, and you may not be able to upgrade to Python3 immediately, but start writing code that is compatible with Python3 now and upgrading to python3 when conditions are ripe.

Note: The difference between Python2 and Python3

If you are more fully aware of the issues associated with migrating from Python2 to Python3, we recommend reading Porting to Python 3 which is a free Python reading.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: [email protected] and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.