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

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

文件:foo_module.py

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

文件: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)

输出:

<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

这很好,按我的预期工作。 但以下不起作用!

文件: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 !

输出:

<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

但是 sys.modules 似乎在这两种情况下都有 foo_module 的条目! 我错过了什么?

感谢 。

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

你的总代码,已修复:

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