1

I have the following toy code in two files:

File b.py:

def test_b():
    print "b"

File a.py: from b import test_b

def test_a():
    print "a"
    test_b()

Then I run the python REPL:

>>> execfile("a.py")
>>> test_a()
a
b

Then I modify b.py into: def test_b(): print "bb"

And run in the REPL:

>>> execfile("b.py")
>>> test_a()
a
bb

For now it is all fine. Now I modify a.py into:

from b import test_b

def test_a():
    print "aa"
    test_b()

Now I run into the REPL:

>>> execfile("a.py")
>>> test_a()
aa
b

Which is not fine anymore since the REPL got the older version of b.py. Python seems to be doing some caching when loading the files, and my question is: is there a way to force it not to do that? I couldn't find a proper option for the function excefile.

S4M
  • 4,561
  • 6
  • 37
  • 47

1 Answers1

1

According to: https://docs.python.org/2/tutorial/modules.html you can use reload(a) (it must have been imported once before). See the description, it might not be the best solution.

The citation:

Note

For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use reload(), e.g. reload(modulename).

and the description of the function: https://docs.python.org/2/library/functions.html#reload to use with moderation since:

If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

The simplest solution is restarting your interpreter.

Dese
  • 338
  • 1
  • 12