How to reload python module from itself?

Posted by mivulf on Stack Overflow See other posts from Stack Overflow or by mivulf
Published on 2012-12-10T16:28:09Z Indexed on 2012/12/10 17:03 UTC
Read the original article Hit count: 152

Filed under:

I have a python module with a lot of variables. These variables are used and changed by many other modules. And I want to reload this module from itself when it happens (for refresh).

How to do that?

# ==================================
# foo.py

spam = 100

def set_spam(value):
    spam = value
    foo = reload(foo) # reload module from itself

# ==================================
# bar.py

import foo
print foo.spam # I expect 100
# assume that value changes here from some another modules (for example, from eggs.py, apple.py and fruit.py)
foo.set_spam(200)
print foo.spam # I expect 200

© Stack Overflow or respective owner

Related posts about python