How to reload a module in python version 3.3.2

whenever i try to reload the python module in python version 3.3.2 i get this error code

>>> import bigmeesh >>> bob=bigmeesh.testmod() this baby is happy >>> imp.reload(bigmeesh) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> imp.reload(bigmeesh) NameError: name 'imp' is not defined 

I tried to research and received no answers.

+7
python
source share
2 answers

You must import imp before you can use it, like any other module:

 >>> import bigmeesh >>> import imp >>> imp.reload(bigmeesh) 

Please note that the documentation clearly says:

Note. Newer programs should use importlib , not this module.

However, in 3.3, importlib does not have a simple reload function; You will have to build it yourself from importlib.machinery . So for 3.3, stick with imp . But in 3.4 and later versions that have importlib.reload , use this instead.


It's also worth noting that reload often not what you want. For example, if you expected bob change to an instance of the new version of bigmeesh.testmod() , this will not happen. But, on the other hand, if you expected that it will not change at all, you may be surprised, because some of its actions may depend on the changed global variables.

+12
source share

This is a modern way to reload a module:

 # Reload A Module def modulereload(modulename): import importlib importlib.reload(modulename) 

Just enter modulereload(MODULE_NAME) , replacing MODULE_NAME with the name of the module you want to reload.

For example, modulereload(math) reloads a math function.

0
source share

All Articles