Python reload (module) usage example details, pythonreload
Preface
This article mainly introduces the reload (module) usage in python and shares it for your reference. I will not talk about it much below. Let's take a look at the detailed introduction.
1. Differences in reload () usage between Python2 and Python3.
In Python2, you can directly use reload (module) to overload the module.
Use the following method in Pyhton3:
(1)
>>> from imp >>> imp.reload(module)
(2)
>>> from imp import reload >>> reload(module)
2. Cause of error when using import and reload () in Python3
Assume that recommendations. py is stored in the C: \ Python34 \ PCI_Code \ chapter2 \ directory, which contains the critics function.
If the following error occurs during the import function,
>>> from recommendation import critics Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> from recommendation import critics ImportError: No module named 'recommendation'
Add the directory C: \ Python34 \ PCI_Code \ chapter2 \ To the system path.
>>> import sys >>> sys.path.append("C:\Python34\PCI_Code\chapter2")
>>> from recommendations import critics >>>
Usereload()
The following error occurs:
>>> from imp import reload >>> reload(recommendations) Traceback (most recent call last): File "<pyshell#86>", line 1, in <module> reload(recommendations) NameError: name 'recommendations' is not defined
The reason is thatimport reload
After that, you need to load the module in the import. At this time, reload will not be a problem. For details, refer to the following code:
>>> from imp import reload >>> import recommendations >>> reload(recommendations) <module 'recommendations' from 'C:\\Python34\\PCI_Code\\chapter2\\recommendations.py'> >>>
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.