我编码eg1.py,eg2.py,eg3.py eg3.py导入eg2.py,然后导入eg1.py
当我第一次运行eg3.py时一切都很好 如果我一次又一次地导入它,只有eg3.py运行
我需要一个解决方案。
我会以这样的方式编写eg3.py:
while(1):
import eg2.py
我出错了。请给我一个解决方案。
答案 0 :(得分:7)
导入时,是否要在eg2.py中执行代码?这不是一个好的解决方案。你应该在eg2.py中有一个包含你的代码的函数,然后在你的while循环中执行这个函数。
在eg2.py中:
def my_func():
# do useful stuff
pass
在eg3.py
import eg2
while True:
eg2.my_func()
答案 1 :(得分:1)
咦?你不能循环import
,它们被缓存,因此在第一次迭代之后它不会做任何事情,除了废物循环。
你怎么知道“只有eg3.py”运行?
答案 2 :(得分:0)
如果导入已导入的模块,则不会重新运行该模块中的可执行代码。
E.g:
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> import this
>>>
从sys.modules中删除模块将强制完全重新加载:
E.g:
>>> import sys
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> del(sys.modules["this"])
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>
编辑: 此外,heikogerlach说:你最好在已导入的模块中调用函数,而不是在大多数时间删除/重新加载函数。