Search code examples
pythonpython-3.xpython-modulesys

Why this reload fails with "NameError: name <xxx> is not defined"?


File : foo_module.py

#file: foo_module.py
def foo():
    print("Am foo v1")

File : attempt1.py

#file: attempt1.py
import sys
from time import sleep
from importlib import reload
import foo_module  # <-- import the entire module

print(sys.modules['foo_module']) #Lets see if the module is loaded
while True:
    foo_module.foo()

    #simulate a delay or a condition by when foo_module would have changed
    sleep(2)

    #should reload foo_module to get the latest
    reload(foo_module)

Output:

<module 'foo_module' from 'D:\\pyth\\foo_module.py'>
Am foo v1
Am foo v1
Am foo v1 # I go in around this time and change the foo_module print statement to simulate update to a loaded module
Am foo v2 # Voila ! reload works !
Am foo v2
Am foo v2

This is good and works as I expected. But the following doesnt work !

File : attempt2.py

#file: attempt2.py
import sys
from time import sleep
from importlib import reload
from foo_module import foo  # <-- import specific item only

#Lets see if the module is loaded
print(sys.modules['foo_module']) 

while True:
    foo()

    #simulate a delay or a condition by when foo_module would have changed
    sleep(2)

    #try to reload foo_module to get the latest
    reload(foo_module) #FAILS !

Output:

<module 'foo_module' from 'D:/pyth\\foo_module.py'>  # <-- Module is loaded. isnt it ?
Am foo v1
Traceback (most recent call last):
  File "D:/pyth/attempt2.py", line 10, in <module>
    reload(foo_module)
NameError: name 'foo_module' is not defined

But sys.modules does seem to have the entry for the foo_module in it in both cases ! What am i missing ?


Solution

  • Credits to https://stackoverflow.com/a/46814062/237105.

    # Reload via sys.modules as it is not imported directly
    reload(sys.modules['foo_module'])
    # Then, reimport function
    from foo_module import foo
    

    Total code of yours, fixed:

    import sys
    from time import sleep
    from importlib import reload
    from foo_module import foo  # <-- import specific item only
    
    print(sys.modules['foo_module'])
    while True:
        foo()
    
        # simulate a delay or a condition by when foo_module would have changed
        sleep(2)
    
        # reload foo_module via sys.modules as it is not imported directly
        reload(sys.modules['foo_module'])
    
        # re-import foo to update it.
        from foo_module import foo