Python 从模块导入特定函数时出现问题

Python 从模块导入特定函数时出现问题,python,import,Python,Import,我有两个单独的模块,我正试图将特定的函数导入彼此,但是在helperModule.py模块本身中定义的函数上不断出现导入错误。我做错了什么 helperModule.py from utilsModule import startProcess def getCfgStr(): pass def getCfgBool(): pass def doSomethingElse(): startProcess() from helperModule import getC

我有两个单独的模块,我正试图将特定的函数导入彼此,但是在
helperModule.py
模块本身中定义的函数上不断出现导入错误。我做错了什么

helperModule.py

from utilsModule import startProcess

def getCfgStr():
    pass

def getCfgBool():
    pass

def doSomethingElse():
   startProcess()
from helperModule import getCfgStr, getCfgBool

def startProcess():
    a = getCfgStr()
    b = getCfgBool()
    pass
utilsModule.py

from utilsModule import startProcess

def getCfgStr():
    pass

def getCfgBool():
    pass

def doSomethingElse():
   startProcess()
from helperModule import getCfgStr, getCfgBool

def startProcess():
    a = getCfgStr()
    b = getCfgBool()
    pass
你展示:

from utilsModule import startProcess
它对应于定义了
startProcess
函数的
utilsModule.py
。在该文件的顶部显示:

from helperModule import getCfgStr, getCfgBool
它对应于定义这两个函数的
helperModule.py

这是一个循环输入
utilsModule
utilshelperModule
导入,该模块从
utilsModule
导入。。。你知道我要去哪里


您必须重构并让它们都从第三个文件导入,以防止出现这种情况。

您可以让函数需要它们需要的函数(在运行时而不是在加载时):

或者,您至少可以尽可能晚地进行导入:

def getCfgStr():
    pass

def getCfgBool():
    pass

def doSomethingElse():
   startProcess()

from utilsModule import startProcess

在这里,我们首先让函数存在,然后用来自另一个模块的函数提供它们的全局名称空间

这样,循环导入可能会起作用,因为导入发生得比较晚


不过,只要在导入时没有使用任何受影响的函数,它就可以工作。

您有一个循环导入。A不能从B导入,B从A导入。你需要重构。我很困惑。我认为问题是循环导入错误,但您声称第一个文件名为
helperModule.py
,但第二个文件是从名为
utilshelperModule
的模块导入的。那么,这是由于输入错误而隐藏的循环导入错误,还是您误报了文件名?(由于您只描述了错误,而没有复制粘贴实际错误,因此很难区分两者。)@DSM相同(因此在我的回答中“似乎对应”),但我还是继续回答了。我想如果结果证明我错了,我会得到应得的反对票。@DSM对不起,复制粘贴错误@两个炼金术士修好了!