Five Methods of Overloading python Module

  • 2021-11-01 04:10:34
  • OfStack

Directory environment preparation
Prohibit duplicate imports
Overload module method 1
Overload module method 2
Overload module method 3
Overload module method 4
Overload module method 5

Environmental preparation

Create a new foo folder containing an bar. py file


$ tree foo
foo
 Off-  bar.py

0 directories, 1 file

The content of bar. py is very simple, and only an print statement is written


print("successful to be imported")

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

Prohibit duplicate imports

Because of the existence of sys. modules, when you import an imported module, it has no effect.


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

Overload module method 1

If you are using python2 (remember to add a __init__.py under the foo folder earlier), there is an reload method that can be used directly


>>> 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 many methods. Please see the following for details

Overload module method 2

If you use Python 3.0-- > 3.3, then the imp. reload method can be used


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

However, 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

Overload module method 3

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


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

Overload module method 4

If you know something about the package loader,

You can also use the following methods


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

Overload module method 5

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


>>> 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 noticed that in the previous examples, I used all the from foo import bar In this case, you use the import foo.bar Why is this?

This is because if you use from foo import bar In this way, trying to overload the module by removing sys. modules is invalid.

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


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

The above is the python module reload of the 5 methods of the details, more about the python module reload information please pay attention to other related articles on this site!


Related articles: