Reload module in python

module, python, reload

Solution

You must have a module to reload. when you use the `from foo import bar`, unless `bar` is a module (it looks like it isn't, in your case) you will have to use another import statement.

from myModules.myModule import myClass
# this will cause myModule.py to be evaluated.  only myClass is in scope

from myModules import myModule
# since myModule has already been imported, myModule.py is not evaluated again. 
# but now myModule is in scope.

reload(myModule)
# this will cause myModule.py to be evaluated again.

If, for some reason, you don't want two imports, the already imported module can also be found in `sys.modules`

Problem

When I import a class `MyClass` from a file `myModule.py` from with a `myModules` dictionary i do it like ``` from myModules.myModule import MyClass ``` How to reload this module after I have made changes to the file `myModue.py`? Here are some mistrials: ``` reload(MyClass) # TypeError: reload() argument must be module reload(myModule) # NameError: name 'myModule' is not defined reload(myModules.myModule) # NameError: name 'myModules' is not defined ```

Original source