5

Let's say I have a Python script main.py that imports othermodule.py. Is it possible to write a reload(othermodule) in main.py so that when I make changes to othermodule.py, I can still just reload(main), which will then reload othermodule?

3
  • I believe reloading the main file is a really big mistake... Commented May 21, 2013 at 16:39
  • If you were to reload(main) (which @Bakuriu rightly says is a bad idea), that wouldn't reload(othermodule) by default. What are you trying to achieve here? Auto-reloading of any changed modules? Commented May 21, 2013 at 16:50
  • Yes, sorry I probably didn't explain this very well, but I am just trying to figure out the best way to reload modules in the case that most of my main code is in main.py which I import in the interpreter, but main.py imports othermodule.py, which I might change. Commented May 21, 2013 at 16:56

2 Answers 2

4

Well, it's not quite that simple. Assuming you have a main.py like this...

import time
import othermodule

foo = othermodule.Foo()
while 1:
    foo.foo()
    time.sleep(5)
    reload(othermodule)

...and an othermodule.py like this...

class Foo(object):
    def foo(self):
        print 'foo'

...then if you change othermodule.py to this...

class Foo(object):
    def foo(self):
        print 'bar'

...while main.py is still running, it'll continue to print foo, rather than bar, because the foo instance in main.py will continue to use the old class definition, although you can avoid this by making main.py like this...

import time
import othermodule

while 1:
    foo = othermodule.Foo()
    foo.foo()
    time.sleep(5)
    reload(othermodule)

Point is, you need to be aware of the sorts of changes to imported modules which will cause problems upon a reload().

Might help to include some of your source code in the original question, but in most cases, it's probably safest just to restart the entire script.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, your code example makes sense. The part that I was trying to ask about originally was in regards to the reload(othermodule) in that last snippet you have. So if I reload(main) in the interpreter after changing othermodule.py, would it also reload(othermodule) and commit the changes?
@rottentomato56 Depends on the contents of main.py. Assuming the first two lines are import othermodule and reload(othermodule), then reload(main) should work, but there may be complications, depending on what the actual code is.
"it's probably safest just to restart the entire script" what if this is a server? Can I do anything better than starting a Python script using subprocess and communicating over stdin/stdout?
1

Python already has reload() is that not good enough?

From your comments, it sounds as if you might be interested in the deep reload function in ipython though I would use it with caution.

3 Comments

Oh, does reload() also handle imported modules within the main script?
@rottentomato56 No.reload simply "destroys" the module object in sys.modules and then re-imports it. If this module imports some other module, this second module is not re-imported since it is already present in sys.module.
Thanks for suggestion, I'll look into this more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.