Import

The function extepy.reload() provides advanced module and object reloading capabilities for Python, so that developer can modify code without restarting the interpreter.

It solves key limitations of Python’s built-in reloading functionality by:

  • Supporting all import styles: import X, import A as X, `from A import X, and from A import B as X.

  • Automatically updating references without requiring reassignment.

  • Offering granular control over namespace updates (global and/or local namespaces).

extepy.reload(obj, update_global=True, update_local=True)[source]

Reload objects to pick up the latest implementation.

Parameters:
  • obj (types.ModuleType | object | str) – Object to reload, can be module, function, class, or variable.

  • update_global (bool) – Whether to update references in the global namespace.

  • update_local (bool) – Whether to update references in the local namespace.

Returns:

New object.

Examples: Reload modules and/or objects

import mymodule
# ... modify mymodule.py ...
from extepy import reload
reload(mymodule)  # reload module
reload("mymodule")  # reload module by name
import mymodule as mymodule1
# ... modify mymodule.py ...
from extepy import reload
reload(mymodule1)  # reload module alias
from mymodule import myfunction
# ... modify myfunction() in mymodule.py ...
from extepy import reload
reload(myfunction)  # reload function
from mymodule import myfunction as myfunction1
# ... modify myfunction() in mymodule.py ...
from extepy import reload
reload(myfunction1)  # reload function alias
from mymodule import myfunction as myfunction1, myfunction as myfunction2
# ... modify myfunction() in mymodule.py ...
from extepy import reload
reload(myfunction1)  # update both myfunction1 and myfunction2

Examples: Update selective namespaces

from extepy import reload
myfunction = None  # global reference
def f():
    global myfunction
    from mymodule import myfunction
    reload(myfunction, update_local=False)  # update global reference only
from extepy import reload
def f():
    from mymodule import myfunction
    # ... modify myfunction() in mymodule.py ...
    reload(myfunction, update_global=False)  # update the local reference only
from extepy import reload
def f():
    from mymodule import myfunction
    # ... modify myfunction() in mymodule.py ...
    reload(myfunction, update_global=False)  # Update only the local calc reference