Computer >> Computer tutorials >  >> Programming >> Python

What are common practices for modifying Python modules?


If you are modifying a module and want to test it in the interpreter without having to restart the shell everytime you save that module, you can use the reload(moduleName) function. reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.

For example

>>> import mymodule
>>> # Edited mymodule and want to reload it in this script
>>> reload(mymodule)

Note that the moduleName is the actual name of the module, not a string containing its name. In Python 3, reload was moved from builtins to imp. So to use reload in Python 3, you'd have to write imp.reload(moduleName) and not just reload(moduleName).