在Python中使用两个脚本时,所有导入都在一个脚本中

时间:2015-06-30 09:34:36

标签: python import

这个问题可能在此论坛上得到了回答,但我没有找到它。

所以我的问题如下: 假设我正在使用两个脚本:

#script 1

import script2
reload (script2)
from script2 import *

def thisisatest():
    print "test successfull"
    return ()

def main():
    realtest()
    return ()

main()

#script 2

def realtest():
    thisisatest()
    return()

现在,如果我运行script1,我会收到一条错误消息,指出未定义全局名称“thisisatest”。然而这是最好的()?调用python为我提供了函数帮助。

编辑:

我的问题是:在一个脚本中执行导入部分(对于所有脚本)是否有办法继续处理许多脚本,或者这是不可能的?

提前致谢,

Enzoupi

1 个答案:

答案 0 :(得分:0)

最好完全避免循环依赖。如果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()