美文网首页读书Java进阶之路
Python 炫技操作:模块重载的五种方法

Python 炫技操作:模块重载的五种方法

作者: 狂徒_张三 | 来源:发表于2021-12-17 19:32 被阅读0次

    推荐:Python全栈教程!花了29980买的从入门到精通课程,分享给大家

    环境准备

    新建一个 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'>
    

    重载模块方法四

    如果你对包的加载器有所了解(详细可以翻阅我以前写的文章:https://iswbm.com/84.html

    还可以使用下面的方法

    >>> 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 炫技操作:模块重载的五种方法

        本文链接:https://www.haomeiwen.com/subject/ttnkfrtx.html