Skip to content

egse.reload

cgse-common

This code is part of the cgse-common package.

egse.reload

A slightly better approach to reloading modules and function than the standard importlib.reload() function. The functions in this module are for interactive use in a Python REPL.

  • reload_module(module): reloads the given module and all its parent modules
  • reload_function(func): reloads and returns the given function

Note

It might be possible that after the module or function has been reloaded that an extra import is needed to import the proper module attributes in your namespace.

Module dependency

When you make a change in a module or function that you are not calling directly, but call through another function from another module, you need to reload the module where you made the change and, after that, reload the function that calls that module.

Example
  • module x.a contains function func_a
  • module x.b contains function func_b which calls func_a

when you make a change in func_a you have to reload the module x.a and then reload the function func_b:

from x.b import func_b
func_b()

now make some changes in func_a, then to make those changes known in your session:

reload_module('x.a')
func_b = reload_function(func_b)
func_b()  # will show the changes done in func_a

Functions:

Name Description
reload_function

Reloads and returns the given function. In order for this to work, you should catch the

reload_module

Reloads the given module and all its parent modules. The modules will be reloaded starting

reload_function

reload_function(func)

Reloads and returns the given function. In order for this to work, you should catch the return value to replace the given function with its reloaded counterpart.

This will also work if you import the function again instead of catching it.

Note

that this mechanism doesn't work for functions that were defined in the __main__ module.

Example

func = reload_function(func)
or
reload_function(func)
from egse.some_module import func

Parameters:

Name Type Description Default
func FunctionType

the function that needs to be reloaded

required

Returns:

Type Description
FunctionType

The reloaded function.

Raises:

Type Description
Abort

when the function is not the proper type or when the function is defined in the __main__ module.

reload_module

reload_module(module, walk=True)

Reloads the given module and all its parent modules. The modules will be reloaded starting from their top-level module. Reloading the 'egse.hexapod.symetry.puna' module will reload 'egse', 'egse.hexapod', 'egse.hexapod.symetry', and 'egse.hexapod.symetry.puna' in that order.

Note

If you pass the module argument as a module, make sure the top level module is imported in your session, or you will get a NameError. To prevent this (if you don't want to import the top-level module, pass the module as a string.

Example

import egse
reload_module(egse.system)
or
reload_module('egse.system')

Parameters:

Name Type Description Default
module Union[ModuleType, str]

The module that needs to be reloaded

required
walk bool

walk up the module hierarchy and import all modules [default=True]

True