为什么再次导入模块不更新模块?
Why does importing module again not updates the module?
我正在阅读有关异常处理的教程,不得不在名为 exception.py
的模块中编写这段代码
def convert(s):
try:
x = int(s)
prnit("Conversion done. x=", x) #there is a typo
except ValueError:
print("Failed")
x = -1
return x
然后我从 REPL 中将此函数导入为
from exception import convert
然后做
转换(7.7)
按预期返回 NameError: name 'prnit' is not defined
。
之后我更正了错别字并再次 from exception import convert
。但错误仍然存在。为什么不导入新模块?
我不得不 exit()
然后 REPL 并再次导入它然后它工作正常并且符合预期。
非常简单:模块已缓存,请参阅主题 (5.3.1) 中的 Python documentation。
导入缓存在 Python 中,您可以在 official documentation 中阅读有关内容。
有多种方法可以使导入缓存失效,但强烈建议不要这样做。
我正在阅读有关异常处理的教程,不得不在名为 exception.py
的模块中编写这段代码def convert(s):
try:
x = int(s)
prnit("Conversion done. x=", x) #there is a typo
except ValueError:
print("Failed")
x = -1
return x
然后我从 REPL 中将此函数导入为
from exception import convert
然后做
转换(7.7)
按预期返回 NameError: name 'prnit' is not defined
。
之后我更正了错别字并再次 from exception import convert
。但错误仍然存在。为什么不导入新模块?
我不得不 exit()
然后 REPL 并再次导入它然后它工作正常并且符合预期。
非常简单:模块已缓存,请参阅主题 (5.3.1) 中的 Python documentation。
导入缓存在 Python 中,您可以在 official documentation 中阅读有关内容。
有多种方法可以使导入缓存失效,但强烈建议不要这样做。