python模块重载的方法有哪些?要注意什么?
Admin 2021-08-28 群英技术资讯 1394 次浏览
这篇文章主要介绍python模块重载的方法,下文给大家分享了python模块重载的五种方法,对大家学习和python模块重载具有一定的帮助,感兴趣的朋友可以参考下,接下来我们一起了解看看吧。
新建一个 foo 文件夹,其下包含一个 bar.py 文件
$ tree foo foo └── bar.py 0 directories, 1 file
bar.py 的内容非常简单,只写了个 print 语句
print("successful to be imported")
只要 bar.py 被导入一次,就被执行一次 print
由于有 sys.modules 的存在,当你导入一个已导入的模块时,实际上是没有效果的。
>>> from foo import bar successful to be imported >>> from foo import bar >>>
如果你使用的 python2(记得前面在 foo 文件夹下加一个 __init__.py),有一个 reload 的方法可以直接使用
>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> reload(bar) successful to be imported <module 'foo.bar' from 'foo/bar.pyc'>
如果你使用的 python3 那方法就多了,详细请看下面
如果你使用 Python3.0 -> 3.3,那么可以使用 imp.reload 方法
>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> import imp >>> imp.reload(bar) successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
但是这个方法在 Python 3.4+,就不推荐使用了
<stdin>:1: DeprecationWarning: the imp module is deprecated in favour of importlib;
see the module's documentation for alternative uses
如果你使用的 Python 3.4+,请使用 importlib.reload 方法
>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> import importlib >>> importlib.reload(bar) successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
如果你对包的加载器有所了解
还可以使用下面的方法
>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> bar.__spec__.loader.load_module() successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
既然影响我们重复导入的是 sys.modules,那我们只要将已导入的包从其中移除是不是就好了呢?
>>> import foo.bar successful to be imported >>> >>> import foo.bar >>> >>> import sys >>> sys.modules['foo.bar'] <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> >>> del sys.modules['foo.bar'] >>> >>> import foo.bar successful to be imported
有没有发现在前面的例子里我使用的都是 from foo import bar,在这个例子里,却使用 import foo.bar,这是为什么呢?
这是因为如果你使用 from foo import bar 这种方式,想使用移除 sys.modules 来重载模块这种方法是失效的。
这应该算是一个小坑,不知道的人,会掉入坑中爬不出来。
>>> import foo.bar successful to be imported >>> >>> import foo.bar >>> >>> import sys >>> del sys.modules['foo.bar'] >>> from foo import bar >>>
关于python模块重载的方法就介绍到这,本文实例有一定的借鉴价值,有这方面学习需求的朋友可以参考学习,希望能对大家有帮助,想要了解更多python模块重载的相关内容,大家可以关注其他文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
冒泡排序(Bubble Sort)是一种简单的排序算法。本文将详细为大家讲讲Python实现冒泡排序算法的方法,感兴趣的小伙伴可以跟随小编一起学习一下
大家好,本篇文章主要讲的是用Python画圣诞树代码示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
本文主要介绍了Python曲线平滑的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
质因数就是质数,质因数在数论指能整除给定正整数的质数。除1外,没有其他公共质数因子的两个正整数称为倒数质数。下面,小编就向大家介绍python中分解质因数的方法。
删除列表或者字符串元素的方法不止一种,同样,删除字典元素的方法也不止一种,本文主要介绍python中删除字典元素的四种方法:1、使用del语句;2、使用clear();3、使用pop();4、使用popitem()。感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008