当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
- 都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
- Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
- Suffix expression to infix expression
- Python应用场景多不多?
- CMS垃圾收集器
- Part 1 - Chapter 2 pointer operation
- Queue with two stacks
- Development and deployment of image classifier application with fastai
- EntityFramework Core上下文实例池原理分析
猜你喜欢
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
【200人面试经验】,程序员面试,常见面试题解析
Flink系列(0)——准备篇(流处理基础)
Dynamic programming: maximum subarray
动态规划答疑篇
构造函数和原型
Newbe.ObjectVisitor 样例 1
[cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
Brief VIM training strategy
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
随机推荐
Flink系列(0)——准备篇(流处理基础)
接口测试工具Eolinker进行post请求
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
getBytes之 LengthFieldBasedFrameDecoder服务端解析
RSA非对称加密算法
[200 interview experience], programmer interview, common interview questions analysis
Five factors to consider before choosing API management platform
Classical dynamic programming: longest common subsequence
实现图片的复制
Mycat搭建
C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
VirtualBox install centos7
CMS垃圾收集器
AI人工智能编程培训学什么课程?
Proficient in high concurrency and multithreading, but can't use ThreadLocal?
经典动态规划:最长公共子序列
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
Iterm2 configuration and beautification
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
Octave基本语法