在Python中使用两个脚本时,所有导入都在一个脚本中进行
这个问题以前可能在这个论坛上得到过回答,但我没有找到答案 所以我的问题是: 假设我正在使用两个脚本:在Python中使用两个脚本时,所有导入都在一个脚本中进行,python,import,Python,Import,这个问题以前可能在这个论坛上得到过回答,但我没有找到答案 所以我的问题是: 假设我正在使用两个脚本: #script 1 import script2 reload (script2) from script2 import * def thisisatest(): print "test successfull" return () def main(): realtest() return () main() 以及: 现在,如果我运行script1,
#script 1
import script2
reload (script2)
from script2 import *
def thisisatest():
print "test successfull"
return ()
def main():
realtest()
return ()
main()
以及:
现在,如果我运行script1,我会收到一条错误消息,说没有定义全局名称“ThisisTest”。然而,ThisisTest()是什么?调用python给了我函数帮助
编辑:
我的问题是:在一个脚本中执行导入部分(对于所有脚本)时,是否有一种方法可以处理多个脚本,还是不可能
提前感谢,
Enzopi最好完全避免循环依赖。如果module1从module2导入,则module2不应从module1导入。如果module2中定义的函数需要使用module1中的函数,则可以将该函数作为参数传递 模块1:
from module2 import otherfunc
def myfunc()
print "myfunc"
def main()
otherfunc(myfunc)
main()
模块2:
def otherfunc(thefunc):
print "otherfunc"
thefunc()
不需要该
reload()
调用的可能重复项。您几乎不需要使用reload
,return
不是一个函数。不要使用return()
,除非您想返回一个空元组;只需简单地return
。感谢@Evert提供的信息。实际上,还有一些建议:如果函数没有返回任何内容,并且它正常结束(没有提前返回),那么您甚至不需要return
语句:它在Python中是隐式的。因此,您可以删除这些函数中的最后一行。
def otherfunc(thefunc):
print "otherfunc"
thefunc()