当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Learn volatile, you change your mind, I see
- JVM真香系列:轻松理解class文件到虚拟机(下)
- 新手入坑指南:工作原因“重启”Deepin系统,发现真的香啊
- 动态规划之子序列问题解题模板
- 200 programmers interview experience, all here
- Iptables from introduction to mastery
- 使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
- Looking for better dynamic getter and setter solutions
- Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
- Suffix expression to infix expression
猜你喜欢
Dynamic programming: maximum subarray
Proficient in high concurrency and multithreading, but can't use ThreadLocal?
实验一作业
动态规划设计:最大子数组
Test comparison of three domestic cloud databases
Dynamic planning
JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
Mongodb database
在Python中创建文字云或标签云
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
随机推荐
To introduce to you, this is my flow chart software—— draw.io
. net core cross platform resource monitoring library and dotnet tool
大数据岗位基础要求有哪些?
装饰器(一)
Django's simple user system (3)
Introduction and application of swagger
使用Fastai开发和部署图像分类器应用
寻找性能更优秀的动态 Getter 和 Setter 方案
Regular backup of WordPress website program and database to qiniu cloud
在Python中创建文字云或标签云
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Flink series (0) -- Preparation (basic stream processing)
采用注解+拦截器的方式进行异步执行的实现方式
Dynamic programming: maximum subarray
Octave基本语法
Learn volatile, you change your mind, I see
Iterm2 configuration and beautification
Mycat搭建
Solve the failure of go get download package
Suffix expression to infix expression