模块导入和重载
模块导入通过import语句实现,但是在同一次会话中只运行一次。
若想要再次运行文件,调用imp标准库中的reload函数:
>>> from imp import reload
>>> reload(script1)
Details are shown in the following example:
create a module named 'myfile.py':
title = "The Meaning of Life"
title is the attribute of 'myfile.py'.
通过属性引用:
>>> import myfile
>>> print(myfile.title)
The Meaning of Life
或者以变量title饮用导入字符串:
>>> from myfile import title
>>> print(title)
The Meaning of Life
本文介绍了Python中模块的导入方式及其使用方法,并演示了如何使用imp库中的reload函数来重新加载已导入的模块,以便获取最新的修改内容。

被折叠的 条评论
为什么被折叠?



