当前位置:网站首页>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 ⼊:
# 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]所创,转载请带上原文链接,感谢
边栏推荐
- 200 programmers interview experience, all here
- 综合架构的简述
- Proficient in high concurrency and multithreading, but can't use ThreadLocal?
- Wechat applet related
- Mongodb database
- 都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
- Tasks of the first week of information security curriculum design (analysis of 7 instructions)
- Suffix expression to infix expression
- How to deploy pytorch lightning model to production
- Five design schemes of singleton mode
猜你喜欢
随机推荐
How to deploy pytorch lightning model to production
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
第一部分——第1章概述
【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
国内三大云数据库测试对比
Is parameter passing in go language transfer value or reference?
Dynamic query processing method of stored procedure
Looking for a small immutable dictionary with better performance
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
getBytes之 LengthFieldBasedFrameDecoder服务端解析
Newbe.ObjectVisitor Example 1
EntityFramework Core上下文实例池原理分析
装饰器(一)
Mongodb add delete modify query operation
大数据岗位基础要求有哪些?
选择排序
微信小程序相关
Creating a text cloud or label cloud in Python
JVM真香系列:轻松理解class文件到虚拟机(下)
动态规划答疑篇