我有两个单独的模块,我试图将特定的功能相互导入,但是我在helperModule.py
模块本身定义的函数中不断收到导入错误。我做错了什么?
helperModule.py
from utilsModule import startProcess
def getCfgStr():
pass
def getCfgBool():
pass
def doSomethingElse():
startProcess()
utilsModule.py
from helperModule import getCfgStr, getCfgBool
def startProcess():
a = getCfgStr()
b = getCfgBool()
pass
答案 0 :(得分:2)
你表示:
from utilsModule import startProcess
对应于定义utilsModule.py
函数的startProcess
。在该文件的顶部,显示:
from helperModule import getCfgStr, getCfgBool
对应于定义这两个函数的helperModule.py
。
这是循环导入。从utilsModule
导入的utilshelperModule
utilsModule
导入 j('.modal input[type=text], .modal select').each(function () {
if ('ontouchstart' in document.documentElement) {
var osVar = new UAParser().getOS();
var verticalPos = "";
if (osVar == "iOS") {
j(this).on('focus', function (e) {
verticalPos = j(this).offset().top;
j('.modal').css("position", "absolute");
});
j(this).on('blur', function (e) {
j('.modal').css("position", "fixed");
j(this).scrollTop(verticalPos);
});
}
}
});
...您会看到我要去的地方。
您必须重构并让它们从第三个文件导入以防止这种情况。
答案 1 :(得分:0)
你可以让函数需要他们需要的函数(在运行时而不是在加载时):
def getCfgStr():
pass
def getCfgBool():
pass
def doSomethingElse():
from utilsModule import startProcess
startProcess()
def startProcess():
from helperModule import getCfgStr, getCfgBool
a = getCfgStr()
b = getCfgBool()
或者你至少可以尽可能晚地进行进口:
def getCfgStr():
pass
def getCfgBool():
pass
def doSomethingElse():
startProcess()
from utilsModule import startProcess
和
def startProcess():
a = getCfgStr()
b = getCfgBool()
from helperModule import getCfgStr, getCfgBool
在这里,我们首先将这些函数存在,然后使用其他模块中的函数提供它们的全局名称空间。
这样,循环导入可能会起作用,因为导入发生得很晚。
只要在导入时没有使用任何受影响的功能,它就会起作用。