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
>>> 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
>>> 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 the package loader (for details, you can read the article I wrote before: https://iswbm.com/84.html)
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, 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 found that I used all of the previous examples from foo import bar
, but in this example, I used them. import foo.bar
Why?
This is because if you use from foo import bar
this way, want to remove sys.modules module to override this method is invalid.
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
>>>
I recommend my original " PyCharm Chinese Guide " e-book, which contains a large number (300) of illustrations . It is well-made and worthy of a collection by every Python engineer.
The address is: http://pycharm.iswbm.com