python之模块导入和重载

模块导入和重载

模块导入通过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
原文地址:https://www.cnblogs.com/Christen/p/5183634.html