【发布时间】:2017-11-20 12:52:53
【问题描述】:
我无法理解为什么reload() 在下面的代码中不能正常工作:
# Create initial file content
f = open('some_file.py', 'w')
f.write('message = "First"\n')
f.close()
import some_file
print(some_file.message) # First
# Modify file content
f = open('some_file.py', 'w')
f.write('message = "Second"\n')
f.close()
import some_file
print(some_file.message) # First (it's fine)
reload(some_file)
print(some_file.message) # First (not Second, as expected)
如果我使用外部编辑器手动更改文件some_file.py(在程序运行时),那么一切都会按预期工作。所以我想这可能与同步有关。
环境:Linux、Python 2.7。
【问题讨论】:
-
有趣的是,我无法在 Windows 上的 Python 3.4 上使用
importlib.reload重现这一点。我得到了输出First ; First ; Second。 -
@DeepSpace 在 Python 3.X 上运行良好!
标签: python file python-module