Module: lib.deepreload
Provides a reload() function that acts recursively.
Python’s normal python:reload() function only reloads the module that it’s
passed. The reload() function in this module also reloads everything
imported from that module, which is useful when you’re changing files deep
inside a package.
To use this as your default reload function, type this:
import builtins
from IPython.lib import deepreload
builtins.reload = deepreload.reload
A reference to the original python:reload() is stored in this module as
original_reload, so you can restore it later.
This code is almost entirely based on knee.py, which is a Python re-implementation of hierarchical module import.
9 Functions
- IPython.lib.deepreload.replace_import_hook(new_import)
- IPython.lib.deepreload.get_parent(globals, level)
parent, name = get_parent(globals, level)
Return the package that an import is being performed in. If globals comes from the module foo.bar.bat (not itself a package), this returns the sys.modules entry for foo.bar. If globals is from a package’s __init__.py, the package’s entry in sys.modules is returned.
If globals doesn’t come from a package or a module in a package, or a corresponding entry is not found in sys.modules, None is returned.
- IPython.lib.deepreload.load_next(mod, altmod, name, buf)
mod, name, buf = load_next(mod, altmod, name, buf)
altmod is either None or same as mod
- IPython.lib.deepreload.import_submodule(mod, subname, fullname)
m = import_submodule(mod, subname, fullname)
- IPython.lib.deepreload.add_submodule(mod, submod, fullname, subname)
mod.{subname} = submod
- IPython.lib.deepreload.ensure_fromlist(mod, fromlist, buf, recursive)
Handle ‘from module import a, b, c’ imports.
- IPython.lib.deepreload.deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1)
Replacement for __import__()
- IPython.lib.deepreload.deep_reload_hook(m)
Replacement for reload().
- IPython.lib.deepreload.reload(module, exclude=('_abc', '_ast', '_codecs', '_collections', '_contextvars', '_datetime', '_functools', '_imp', '_io', '_locale', '_opcode', '_operator', '_signal', '_sre', '_stat', '_string', '_suggestions', '_symtable', '_sysconfig', '_thread', '_tokenize', '_tracemalloc', '_types', '_typing', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'sys', 'os.path', 'builtins', '__main__', 'numpy', 'numpy._globals'))
Recursively reload all modules used in the given module. Optionally takes a list of modules to exclude from reloading. The default exclude list contains modules listed in sys.builtin_module_names with additional sys, os.path, builtins and __main__, to prevent, e.g., resetting display, exception, and io hooks.