0

I have a problem in reloading modules that are imported and used in a different module. For clarity, let's consider the example where I have two modules:

First Module: operation.py

def f(x,y):
  return x+y

Second Module: test_operation.py

import operation
x=3
y=4
z= operation.f(x,y)
print z

If I go to the interpreter and write:

import test_operation

I will get 4 printed (z=4). If I change the function f in the first module operation.py to be:

def f(x,y):
  return x+y+1

and then I write in the interpreter reload(test_operation), I will still get 4 printed instead of 5. It seems then that it reloads the module test_operation.py, but it doesn't reload the module operation.py that is imported in test_operation.py.

How do I solve such problem ? and In general case how do I make sure that all imported modules in my code are re-imported (or reloaded) every time they are changed ?

A. Clare
  • 57
  • 5
  • use `reload(operation)`, then `reload(test_operation)`. – Uriel Nov 19 '16 at 21:14
  • you are reloading the already imported module on memory after your change in operation.py not the new one with your edit – Ari Gold Nov 19 '16 at 21:18
  • I understand that I can reload modules one by one by hand, but is there a more automatic method that reload a module and all its imported modules, and goes like this recursively ? Is there a deep reload (like there is a copy and deep copy) ? – A. Clare Nov 19 '16 at 22:04

1 Answers1

0

From the interpreter: just reload operation within test_operation module context using full path:

reload(test_operation.operation)

In test_operation.py, you could call reload(operation) so reloading test_operation would also reload operation

for full recursive reload check here (not sure it's a good idea...): Recursive version of 'reload'

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Yes, indeed what you said works. I checked also the link you shared for the recursive reload, it's an adhoc built recursive function. I thought that there is a built-in method like reload, because it if there are many modules that should be reloaded, and those same modules import other modules that should be reloaded, it could be a slow procedure to do that by hand. Why don't you think that recursive reload is not a good idea ? – A. Clare Nov 19 '16 at 21:45
  • reloading everything may be slow, that's all. – Jean-François Fabre Nov 19 '16 at 22:11
  • Yes, I understand, but is there a built-in function to do that ? Like reload ? (so it will be similar to what is provided by copy and deepcopy) – A. Clare Nov 19 '16 at 22:22