python 模块重载的五种方法

 更新时间:2021年04月23日 16:53:43   作者:写代码的明哥  
这篇文章主要介绍了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 模块重载的资料请关注脚本之家其它相关文章!

相关文章

  • pandas 层次化索引的实现方法

    pandas 层次化索引的实现方法

    这篇文章主要介绍了pandas 层次化索引的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Python中使用Chaco绘图库

    Python中使用Chaco绘图库

    这篇文章主要介绍了Python中使用Chaco绘图库,Chaco是一个2D的绘图库,如果你安装了Python(x,y)的话,可以在pythonxy的安装目录下的找到Chaco的demo程序,Chaco提供了类似Matlab和pylab的绘图方式,我们称之为面向脚本的绘图方式
    2023-11-11
  • pycharm 实现本地写代码,服务器运行的操作

    pycharm 实现本地写代码,服务器运行的操作

    这篇文章主要介绍了pycharm 实现本地写代码,服务器运行的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • python实现微信发送邮件关闭电脑功能

    python实现微信发送邮件关闭电脑功能

    这篇文章主要介绍了python实现微信发送邮件关闭电脑功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 如何利用pygame实现简单的五子棋游戏

    如何利用pygame实现简单的五子棋游戏

    这篇文章主要给大家介绍了关于如何利用pygame实现简单的五子棋游戏的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用pygame具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • Python cx_freeze打包工具处理问题思路及解决办法

    Python cx_freeze打包工具处理问题思路及解决办法

    这篇文章主要介绍了Python cx_freeze打包工具处理问题思路及解决办法的相关资料,需要的朋友可以参考下
    2016-02-02
  • Python表格处理模块xlrd在Anaconda中的安装方法

    Python表格处理模块xlrd在Anaconda中的安装方法

    本文介绍在Anaconda环境下,安装Python读取.xls格式表格文件的库xlrd的方法,xlrd是一个用于读取Excel文件的Python库,本文介绍了xlrd库的一些主要特点和功能,感兴趣的朋友一起看看吧
    2024-04-04
  • python实现opencv+scoket网络实时图传

    python实现opencv+scoket网络实时图传

    这篇文章主要为大家详细介绍了python实现opencv+scoket网络实时图传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • Python  pandas中的shift位移操作方法

    Python  pandas中的shift位移操作方法

    shift() 函数是 Pandas 中用于移动或偏移数据的重要工具,它可以处理时间序列数据、计算数据差值以及进行数据预处理,本文介绍Python  pandas中的shift位移操作方法,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • 在Python中使用成员运算符的示例

    在Python中使用成员运算符的示例

    这篇文章主要介绍了在Python中使用成员运算符的示例,是Python学习中的基础知识,需要的朋友可以参考下
    2015-05-05

最新评论