Reloading Python Packages

Viewed 115

the main module sits within the runner package and executes stuff in the other packages. The main module can also Update the other packages and when that happens I want to reload them in order to get the new functions/modules that were added to those packages.

Project Structure

|--runner
|----main.py
|--core
|----module_1.py
|--configurations
|--utils

But that doesn't work.

I tried the following commands:

  1. importlib.reload - only reloads a single module, using it recursively with sys.modules didn't add the new modules to the import tree. example: if after the update, "core" received a new module "module_new.py" and its imported in "module_1.py" it's not recognized after the reload.
  2. I tried using IPython.lib.deepreload - it didn't work as well.

I've been stuck with this issue for some time, and haven't found any working solution yet. Suggestions? Thanks

2 Answers

I fixed the issue by restarting the entire program using a while loop from an outer execution script. Exit code 2: update required

    Do
    {
        $process = Start-Process python -ArgumentList $CommandLine -verb RunAs -PassThru -WindowStyle Minimized -Wait
    } WHILE ($process.ExitCode -eq 2)

Modules will be reloaded by import command if they are not in sys.modules dict

# import some standard (non-updatable) modules
import numpy as np

# save set of non-reloadable modules on first run,
# and delete reloadable modules on other runs
if 'init_modules' not in globals():
    init_modules = set(sys.modules.keys())
else:
    modules = list(sys.modules.keys())
    for m in modules:
        if m not in init_modules:
            del(sys.modules[m])

# import reloadable packages and modules
import MyPackage
Related