Make your python program compatible with python2 and python3 at the same time

Source: Internet
Author: User
You only need to make some modifications to your code to support Python 2 and Python 3 at the same time. Next I will briefly introduce how to make your python code support both py... you only need to make some modifications to your code to support Python 2 and Python 3 at the same time. Next, I will briefly introduce How to Make python code support python 2 and python 3 at the same time.


Discard python versions earlier than python 2.6

Python versions earlier than python 2.6 lack some new features, which may cause a lot of trouble for your migration. If you do not have to give up the support for the previous version.

Use 2to3 tool to check the code

2to3 is a built-in python code conversion tool that can automatically convert python 2 code to python 3 code. Of course, unfortunately, the converted code is not compatible with Python 2. Therefore, we do not actually use 2to3 to convert the code. Run 2to3 t. py to view the output information and correct the problem.

Use python-3 to execute the python program

2to3 can check for many python2 & 3 compatibility problems, but there are also many problems that 2to3 cannot find. After the-3 parameter is added, the python2 and python3 are inconsistent on the console during running, and the 2to3 cannot be handled. For example, the rules for division are changed in python3 and python2. If you run the 4/2 command with the-3 parameter, the message "DeprecationWarning: classic int pision" is displayed.

From _ future _ import

After "from _ future _ import", you can use the future features of python. For the full future feature of python, see _ future __. All characters in python3 are converted to unicode. In python2, unicode characters must be prefixed with u During definition, but in 3, they do not need home u, and after u is added, the program cannot be compiled. To solve this problem, you can "from Ure import unicode_literals". In this way, the characters in python2 will be consistent with those in python3, and the general characters defined in python2 will be automatically recognized as unicode.

Import problems

In python3, many python2 packages are missing. In most cases, these packages have changed their names. We can solve these problems during import.

Try: # python2

From UserDict import UserDict

# We recommend that you follow the python3 name for import.

From UserDict import DictMixin as MutableMapping

Failed T ImportError: # python3

From collections import UserDict

From collections import MutableMapping

Write a program in python3 mode

In python2, print is the keyword. in python3, print becomes a function. In fact, the print function is already included in python2.6, so you can simply change the print function to a new one as prompted in 2to3. Some changes have been made to the exception handling in python3. This is similar to print. just follow the instructions in 2to3 to modify it.

Check the currently running python version

Sometimes you may have to write different code 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 encapsulate the differences between Python 2 and Python 3. I do not recommend six. If python versions earlier than python2.6 are not supported, it is easier to handle compatibility issues even if six is not used. Using six will make your code more like python2 rather than python3.

The popularity of python3 requires the promotion of every pythoner. you may not be able to upgrade to python3 immediately, but please start to write code compatible with python3 and upgrade to python3 when conditions are ripe.

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.