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

Source: Internet
Author: User

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

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.

  • Discard python versions earlier than python 2.6Python 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 code2to3 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. Run2to3 t. pyView output information and correct related problems.
  • Use python-3 to execute the python Program2to3 can check for many python2 & 3 compatibility problems, but there are also many problems that 2to3 cannot find. Add-3When the program is running, the python 2 and Python 3 are inconsistent on the console, and 2to3 cannot handle the problem. 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 _ importAfter "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 added before the characters during definition.UBut in 3, there is no need for home u, and after u is added, the program cannot be compiled. To solve this problem, you can "fromFutureImport unicode_literals ", so that the character behavior in python2 will be consistent with that in python3, and the general character defined in python2 will be automatically recognized as unicode.
  • Import ProblemsIn 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 # It is recommended to import from UserDict import DictMixin as mutablemappingt t ImportError: # python3 from collections import UserDict from collections import MutableMapping
  • Write a program in python3 ModeIn 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 versionSometimes 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 = False
  • SixSix 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.

Note:

  • Differences between python2 and python3
  • If you want to learn more about the migration from python2 to python3, we recommend that you read Porting to Python 3, a free python book.

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.