Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload a python submodule?

Tags:

python

I'm loading a submodule in python (2.7.10) with from app import sub where sub has a config variable. So I can run print sub.config and see a bunch of config variables. Not super complex.

If I change the config variables in the script, there must be a way to reload the module and see the change. I found a few instructions that indicated that reload(app.sub) would work, but I get an error:

NameError: name 'app' is not defined

And if I do just reload(sub) the error is:

TypeError: reload() argument must be module

If I do import app I can view the config with print app.sub.config and reload with reload(app)

-- if I do import app and then run

I found instructions to automate reloading: Reloading submodules in IPython

but is there no way to reload a submodule manually?

like image 597
Amanda Avatar asked Feb 25 '16 23:02

Amanda


1 Answers

With python3,I try this:

import importlib
import sys

def m_reload():
    for k,v in sys.modules.items():
        if k.startswith('your-package-name'):
            importlib.reload(v)
like image 174
放課後 Avatar answered Oct 09 '22 19:10

放課後