10

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?

3
  • 1
    What version of Python? Commented Feb 25, 2016 at 23:41
  • 2
    @KarenClark: It must be Python2, because reload() isn't around in Python3. Commented Feb 25, 2016 at 23:51
  • (edited to answer your Qs.) Commented Feb 29, 2016 at 19:14

4 Answers 4

11

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This caused RuntimeError: dictionary keys changed during iteration on my end. So I iterate over a copy of the dict items for k,v in list(sys.modules.items()):
0

When you from foo import bar, you now have a module object named bar in your namespace, so you can

from foo import bar

bar.do_the_thing()  # or whatever

reload(bar)

If you want some more details on how different import forms work, I personally found this answer particularly helpful, myself.

1 Comment

That approach always gives me an error: "TypeError: reload() argument must be module"
0
from importlib import reload
import sys
ls=[]
#making copy to avoid regeneration of sys.modules
for i,j in sys.modules.items():
    r,v=i,j
    ls.append((r,v))

for i in ls:
    if i[0] == 'my_library_name':
        reload(i[1])

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

@6c1 answer did not work for me.

Here is a modification of it that did work for me.

from foo import bar
bar.do_the_thing()

del bar
import sys
import importlib
importlib.reload(sys.modules['foo'])
from foo import bar

This is useful in a workflow like the following:

  1. open python terminal
  2. you load bar (i.e. from foo import bar)
  3. you realize you need to make changes to bar
  4. you make changes to bar
  5. you want to use the new bar (i.e. you want to reload bar) without closing the existing python terminal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.