当前位置:网站首页>How to make scripts compatible with both Python 2 and python 3?

How to make scripts compatible with both Python 2 and python 3?

2020-11-08 21:08:00 xxxxxx

   The problem developers often encounter is , Yes ⼀ individual ⾮ Often famous Python Modules are used by many developers ⽤ the , But not all ⼈ They only make ⽤Python2 perhaps Python3. At this point, you have two ways . The first ⼀ One way is to develop two modules , in the light of Python2⼀ individual , in the light of Python3⼀ individual . also ⼀ One way is to adjust your current code to be compatible at the same time Python2 and Python3. This article will introduce ⼀ Some ways , Make your scripts compatible at the same time Python2 and Python3.

  Future Module leads to ⼊

   That's the most important thing ⽅ Law , That is to guide ⼊__future__ modular . It can help you in Python2 Midcourse ⼊Python3 The function of . There are ⼀ Group example ⼦. Up and down ⽂ The manager is Python2.6+ lead ⼊ New features , If you want to Python2.5 Chinese envoy ⽤ It can do this :

  from __future__ import with_statement

   stay Python3 in print Has changed to ⼀ A function . If you want to Python2 Chinese envoy ⽤ It can go through __future__ guide ⼊:

  print

  # Output:

  from __future__ import print_function

  print(print)

  # Output:

   Module renaming

  ⾸ First ,⼤ Most of the time, we'll do this in scripts ⼊ modular :

  import foo

  # or

  from foo import bar

   You can do the same :

  import foo as foo

   This can work and ⾯ Code has the same function , But the most important thing is that it makes your scripts compatible at the same time Python2 and Python3. Now let's take a look ⾯ Code for :

  try:

  import urllib.request as urllib_request # for Python 3

  except ImportError:

  import urllib2 as urllib_request # for Python 2

   A little explanation ⼀ Down and up ⾯ Code for . We will lead the module to ⼊ The code is packaged in try/except In the sentence . We do this because of Python 2 Not in China urllib.request modular . This will cause ⼀ individual ImportError abnormal .⽽ stay Python2 in urllib.request The function of urllib2 Provided . therefore , When we try to be in Python2 Midcourse ⼊urllib.request Module time ,⼀ Once we catch ImportError We will guide ⼊urllib2 Module to replace it .

   Last , You need to understand as Key words ⽤. It will lead to ⼊ Module mapping to urllib.request, So we went through urllib_request This alias can make ⽤urllib2 All classes in and ⽅ Yes .

   overdue Python2 The built-in function

   another ⼀ One thing to know is Python2 There is 12 Built in features in Python3 Has been removed . Make sure that Python2 Don't use these functions in the code to ensure that it is correct for Python3 Compatibility . There are ⼀ A compulsion to give up 12 Built in features ⽅ Law :

  from future.builtins.disabled import *

   Now? , As long as you try to be in Python3 Chinese envoy ⽤ These abandoned modules are , Will throw ⼀ individual NameError abnormal , as follows :

  from future.builtins.disabled import *

  apply()

  # Output: NameError: obsolete Python 2 builtin apply is disabled

   Standard library down compatible with external ⽀ a

   Yes ⼀ Some bags are in ⾮ Officer, ⽅ Of ⽀ Hold on to Python2 Provides Python3 The function of . For example, we have :

  enum pip install enum34

  singledispatch pip install singledispatch

  pathlib pip install pathlib

   That's all the way , See here , I believe you have already understood how to make scripts compatible at the same time Python2 and Python3. If you want to learn more about Python Dry goods of , You can find a lot of high quality Python Online courses , Let's see ~ Zhengzhou infertility hospital which good :http://yyk.39.net/zz3/zonghe/fc964.html Which is the best hospital in Zhengzhou :http://yyk.39.net/zz3/zonghe/1d426.html Zhengzhou gynecological hospital :http://jbk.39.net/yiyuanfengcai/yyjs_sysdfkyy/

版权声明
本文为[xxxxxx]所创,转载请带上原文链接,感谢