当前位置:网站首页>如何让脚本同时兼容Python2和Python3?
如何让脚本同时兼容Python2和Python3?
2020-11-08 21:08:00 【sxxbxh】
程序开发者常常会遇到的问题是,有⼀个⾮常出名的Python模块被很多开发者使⽤着,但并不是所有⼈都只使⽤Python2或者Python3。这时候你有两个办法。第⼀个办法是开发两个模块,针对Python2⼀个,针对Python3⼀个。还有⼀个办法就是调整你现在的代码使其同时兼容Python2和Python3。本文将介绍⼀些方法,让你的脚本同时兼容Python2和Python3。
Future模块导⼊
这也是最重要的⽅法,就是导⼊__future__模块。它可以帮你在Python2中导⼊Python3的功能。这有⼀组例⼦。上下⽂管理器是Python2.6+引⼊的新特性,如果你想在Python2.5中使⽤它可以这样做:
from __future__ import with_statement
在Python3中print已经变为⼀个函数。如果你想在Python2中使⽤它可以通过__future__导⼊:
# Output:
from __future__ import print_function
print(print)
# Output:
模块重命名
⾸先,⼤多时候我们会这样在脚本中导⼊模块:
import foo
# or
from foo import bar
其实你也可以这样做:
import foo as foo
这样做可以起到和上⾯代码同样的功能,但最重要的是它能让你的脚本同时兼容Python2和Python3。现在我们来看下⾯的代码:
try:
import urllib.request as urllib_request # for Python 3
except ImportError:
import urllib2 as urllib_request # for Python 2
稍微解释⼀下上⾯的代码。我们将模块导⼊代码包装在try/except语句中。我们是这样做是因为在Python 2中并没有urllib.request模块。这将引起⼀个ImportError异常。⽽在Python2中urllib.request的功能则是由urllib2提供的。所以,当我们试图在Python2中导⼊urllib.request模块的时候,⼀旦我们捕获到ImportError我们将通过导⼊urllib2模块来代替它。
最后,你要了解as关键字的作⽤。它将导⼊的模块映射到urllib.request,所以我们通过urllib_request这个别名就可以使⽤urllib2中的所有类和⽅法了。
过期的Python2内置功能
另⼀个需要了解的事情就是Python2中有12个内置功能在Python3中已经被移除了。要确保在Python2代码中不要出现这些功能来保证对Python3的兼容。这有⼀个强制让你放弃12内置功能的⽅法:
from future.builtins.disabled import *
现在,只要你尝试在Python3中使⽤这些被遗弃的模块时,就会抛出⼀个NameError异常,如下:
from future.builtins.disabled import *
apply()
# Output: NameError: obsolete Python 2 builtin apply is disabled
标准库向下兼容的外部⽀持
有⼀些包在⾮官⽅的⽀持下为Python2提供了Python3的功能。例如我们有:
enum pip install enum34
singledispatch pip install singledispatch
pathlib pip install pathlib
以上就是全部的方法,看到这里,相信大家已经明白了如何让脚本同时兼容Python2和Python3。如果要想学习更多关于Python的干货,可以在博学谷找到很多优质的Python在线课程,一起来看看吧~郑州不孕不育医院哪家好:http://yyk.39.net/zz3/zonghe/fc964.html郑州人流医院哪家好:http://yyk.39.net/zz3/zonghe/1d426.html郑州妇科医院:http://jbk.39.net/yiyuanfengcai/yyjs_sysdfkyy/
版权声明
本文为[sxxbxh]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4696788/blog/4707576
边栏推荐
猜你喜欢

使用Fastai开发和部署图像分类器应用

线程池运用不当的一次线上事故

Express framework

To introduce to you, this is my flow chart software—— draw.io

APReLU:跨界应用,用于机器故障检测的自适应ReLU | IEEE TIE 2020
![[200 interview experience], programmer interview, common interview questions analysis](/img/fb/625e17f83f6be064f7387e78ec082a.jpg)
[200 interview experience], programmer interview, common interview questions analysis

Django之简易用户系统(3)

Programmers should know the URI, a comprehensive understanding of the article

200人的程序员面试经验,都在这里了

Brief introduction of Integrated Architecture
随机推荐
If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
MongoDB数据库
CMS垃圾收集器
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
Learn volatile, you change your mind, I see
go语言参数传递到底是传值还是传引用?
Implementation of warehouse management system with ABP (net core) + easyUI + efcore
JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
getBytes之 LengthFieldBasedFrameDecoder服务端解析
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
Regular backup of WordPress website program and database to qiniu cloud
Deep copy
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
SQL 速查
git操作与分支管理规范
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
在Python中创建文字云或标签云
net.sf.json . jsonobject's format processing of time stamp
C/C++学习日记:原码、反码和补码