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

Source: Internet
Author: User

Someone in the python email list said, "python3 cannot be popularized within 10 ". In my opinion, this is too pessimistic. Although python3 and python2 are incompatible, the difference between them is not as big as many people think. 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. Giving up python versions earlier than python 2.6, python versions earlier than python 2.6, lacks some new features and 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 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. Using python-3 to execute the python program 2to3 can check the compatibility of many python 2 & 3 problems, but many problems cannot be found in 2to3. 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 division is displayed. From _ future _ import "from _ future _ import" 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. In python3, many Python 2 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 # It is recommended to import from UserDict import DictMixin as mutablemappingt t ImportError according to the name of python3: # python3 from collections import UserDict from collections import MutableMapping use python3 to write the print keyword in the program python2. 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 current 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 sysif sys. version> '3': PY3 = Trueelse: PY3 = Falsesixsix 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.