Module Reloading

The built-in function reload() can be used to reload and execute the code contained within a module previously loaded with import. It accepts a module object as a single argument. For example:

import foo
... some code ...
reload(foo)         # Reloads foo

All operations involving the module after the execution of reload() will utilize the newly loaded code. However, reload() doesn’t retroactively update objects created using the old module. Therefore, it’s possible for references to coexist for objects in both the old and new versions of a module. Furthermore, compiled extensions written in C or C++ cannot be reloaded using reload().

As a general rule, avoid module reloading except during debugging and development.

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.