Five methods of python module overloading

Environmental preparation

Create a new folder foo, which contains a bar.py file

$ tree foo
foo
└── bar.py

0 directories, 1 file

The content of bar.py is very simple, just write a print statement

print("successful to be imported")

As long as bar.py is imported once, it will be executed once print

No duplicate import

'Because of the existence of sys.modules, when you import an imported module, it is actually ineffective. '

>>> from foo import bar
successful to be imported
>>> from foo import bar
>>>

Repeat import method one

If you use python2 (remember to add one under the foo folder before __init__.py), there is a reload method that can be used directly

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> 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'>

If you use python3, there are more methods, please see below for details

Repeat import method two

If you use Python3.0 -> 3.3, then you can use imp.reload method

>>> 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'>

But this method is not recommended in Python 3.4+

<stdin>:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

Repeat import method three

If you are using Python 3.4+, please use the importlib.reload method

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> 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'>

Repeat import method four

If you know something about package loader

You can also use the following method

>>> 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'>

Repeat import method five

Since it is sys.modules that affects our repeated import, can we just remove the imported package from it?

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> 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

Have you found that in the previous example I used from foo import bar, but in this example, I used import foo.bar? Why?

This is because if you use from foo import bar this method, it is invalid to use the method of removing sys.modules to reload the module.

This should be regarded as a small pit. People who don’t know will fall into the pit and cannot climb out.

>>> import foo.bar
successful to be imported
>>>
>>> import foo.bar
>>>
>>> import sys
>>> del sys.modules['foo.bar']
>>> from foo import bar
>>>

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/109313011