在 Python 中使用两个脚本时全部导入一个脚本

All import in one script when working with two scripts in Python

这个问题可能已经在这个论坛上回答过了,但我没能找到它。

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

#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" 未定义。然而,thisisatest()?调用 python 给我功能帮助。

编辑:

我的问题是:有没有办法在一个脚本中执行导入部分(针对所有脚本)的同时处理多个脚本,还是不可能?

提前致谢,

恩佐皮

最好完全避免循环依赖。如果 module1 从 module2 导入,则 module2 不应从 module1 导入。如果模块 2 中定义的函数需要使用模块 1 中的函数,您可以将该函数作为参数传递。

模块 1:

from module2 import otherfunc

def myfunc()
    print "myfunc"

def main()
    otherfunc(myfunc)

main()

模块 2:

def otherfunc(thefunc):
    print "otherfunc"
    thefunc()