Home Backend Development Python Tutorial Make your python program compatible with both python2 and python3

Make your python program compatible with both python2 and python3

Oct 18, 2016 pm 12:00 PM
python2 python3 compatible

You only need to make slight modifications to your code to support both python2 and python3. Below I will briefly introduce how to make your python code support both python2 and python3.


Abandon python versions before python 2.6

Python versions before python 2.6 lack some new features, which will bring a lot of trouble to your migration work. If you don't have to, give up support for previous versions.

Use the 2to3 tool to check the code

2to3 is a code conversion tool that comes with python, which can automatically convert python2 code into python3 code. Of course, unfortunately, the converted code does not do anything for python2 compatibility. So we don't really use the 2to3 converted code. Execute 2to3 t.py to view the output information and correct related problems.

Use python -3 to execute python programs

2to3 can check out many python2&3 compatibility issues, but there are also many issues that 2to3 cannot find. After adding the -3 parameter, the program will prompt on the console that python2 and python3 are inconsistent, and that 2to3 cannot handle it. For example, the division processing rules have been changed in python3 and python2. Executing 4/2 with the -3 parameter will prompt DeprecationWarning: classic int division .

from __future__ import

"from __future__ import" can be used to use the future features of python. Python's complete future features can be found in __future__ . All characters in python3 have become unicode. In python2, unicode characters need to be added with u in front of the character when defining, but in python 3, u is not required, and the program will not be compiled after adding u. In order to solve this problem, you can "from future import unicode_literals", so that the behavior of characters in python2 will be consistent with that in python3, and ordinary characters defined in python2 will be automatically recognized as unicode.

Import problem

There are "missing" many python2 packages in python3. In most cases, the names of these packages have just been changed. We can deal with these problems when importing.

try:#python2

from UserDict import UserDict

#It is recommended to import according to the name of python3

from UserDict import DictMixin as MutableMapping

except ImportError: #python3

from collections import UserDict

from collections import MutableMapping

Use python3 to write programs

In python2, print is a keyword, but in python3, print becomes a function. In fact, the print function is already included in python2.6, so for print, you can just follow the tips given in 2to3 and change it to the new way of writing. Some changes have been made to exception handling in python3. This is similar to print. You can modify it directly according to the prompts in 2to3.

Check the currently running python version

Sometimes you may have to write different codes 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 package between Python 2 and Python 3 differences between. I don't really recommend using six. If you don't need to support python versions before python2.6, it is easier to deal with compatibility issues even without six. Using six will make your code more like python2 than python3.

The popularity of python3 needs the promotion of every pythoner. Maybe you can’t upgrade to python3 immediately, but please start writing python3-compatible code now and upgrade to python3 when conditions are ripe.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The most stable version of Win10 The most stable version of Win10 Dec 25, 2023 pm 07:58 PM

The most stable version of Win10

Does Win11 support dynamic wallpapers? Does Win11 support dynamic wallpapers? Jan 01, 2024 pm 06:41 PM

Does Win11 support dynamic wallpapers?

Is Switch2 compatible with Switch cartridges? Is Switch2 compatible with Switch cartridges? Jan 28, 2024 am 09:06 AM

Is Switch2 compatible with Switch cartridges?

How to install pip in python2 How to install pip in python2 Dec 07, 2023 pm 04:13 PM

How to install pip in python2

Introduction to the setting method of win10 compatible with win7 Introduction to the setting method of win10 compatible with win7 Jan 03, 2024 pm 05:09 PM

Introduction to the setting method of win10 compatible with win7

Can programs compiled by Mingw run normally under Linux systems? Can programs compiled by Mingw run normally under Linux systems? Mar 19, 2024 pm 12:03 PM

Can programs compiled by Mingw run normally under Linux systems?

Methods to solve the incompatibility of d3dx9_43.dll Methods to solve the incompatibility of d3dx9_43.dll Feb 24, 2024 pm 10:06 PM

Methods to solve the incompatibility of d3dx9_43.dll

How to deal with Eclipse version incompatibility How to deal with Eclipse version incompatibility Jan 04, 2024 am 10:29 AM

How to deal with Eclipse version incompatibility

See all articles