Let python be compatible with python2 and python3 8 tips at the same time, python2python3

Source: Internet
Author: User

Let python be compatible with python2 and python3 8 tips at the same time, python2python3

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.

I. 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.

2. 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.

3. 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 division is displayed.

4. 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.

V. 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.
Copy codeThe Code is as follows: 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

6. Use python3 to write programs

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.

7. 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.
Copy codeThe Code is as follows: import sys
If sys. version> '3 ':
PY3 = True
Else:
PY3 = False

VIII. 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.

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.


Python with versions python27 and python33 are installed. The Development Environment wing 101 IDE is used.

Wing can specify python2.7 in the path in settings. Check the settings. I have set it before.
You can also change the registry.
Find two registration items in HKEY_class_root.
Python point CompiledFile
Python Point File
Change the path in the subitem shell open to the 2.7 path.
There are several other interpreters later, which have been changed by the way.
Of course there is another stupid way. You can reinstall python2.7 and choose repair and installation.

Wing 101 I used it. Although several key functions are missing from the official version. However, the interface is still neat. It seems stable.

Wing often exits illegally.

Which python version does the basic python tutorial support?

The basic tutorial corresponds to python2.6 which can be downloaded from the official python website.
Www.python.org/getit/
Python3.x is significantly changed compared with 2.x
If you use windows, you can download Python 2.7.2 Windows Installer
The difference between 2.7.2 and 2.6 is not very big. It has some characteristics of 3. x.

Install next one way. I will not talk about it in the 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.