Make your Python program compatible with both Python2 and Python3

Source: Internet
Author: User

This article reproduced with https://haoluobo.com/

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.

    • Discard Python versions 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.
    • Checking code with the 2to3 tool
      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.
    • Using python-3 to execute Python programs
      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.
        • from __future__ import
          " from __future__ import " will then make use of Python's future features. Python's full future features are visible __future__ . All the characters in the Python3 become Unicode. Unicode characters in Python2 need to precede characters with uin 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.
        • Import issue
          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.

      1234567 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

        • Write a program using the Python3 method
          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.
        • Check 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.

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

        • 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.
        • Python3 's performance problem has always been a lot of pythoner very tangled, but the latest python3.3 should already have good performance. Reference: Benchmarking python 3.3 against Python 2.7

Make your Python program compatible with both Python2 and Python3

Related Article

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: info-contact@alibabacloud.com 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.