You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python跨模块调用遇NameError:'startMining'未定义求助

Python/SikuliX模块循环导入导致的NameError问题

问题描述

我是Python和SikuliX新手,目前遇到模块间函数调用问题:有三个Python文件main.py、checkInventoryStatus.py、startMining.py,其中checkInventoryStatus.py定义了checkInventoryStatus()函数,startMining.py定义了startMining()函数。

在main.py中通过from checkInventoryStatus import *可正常调用checkInventoryStatus(),但当该函数尝试调用startMining()时,出现错误:NameError (global name 'startMining' is not defined)。我已在checkInventoryStatus.py开头添加from startMining import *,但问题依旧。


代码展示

main.py

from startMining import *
from watchMining import *
from checkInventoryStatus import *

def startApp():
    KO = switchApp("KL")
    checkInventoryStatus()


with open("C:\Users\PC\Desktop\skx\logs.txt", "w") as f:
    f.write("App started!\n")
startApp()

checkInventoryStatus.py

from sikuli import *
from startMining import *
from watchMining import *

def checkInventoryStatus():
    if(exists("1673723539914.png")):
        if(exists(Pattern("1673723411692.png").exact())):
            startMining()

startMining.py

from sikuli import *
from watchMining import *
from checkInventoryStatus import *

def startMining():
    wait(0.6)
    type(Key.ENTER + "/town" + Key.ENTER)
    arrived = False
    wait(0.3)
    
    try:
        click(Pattern("1673722724479.png").similar(0.58))
        wait(1.5)
        while(True):
                if(exists(Pattern("1673729480660.png").exact())):
                    click(Pattern("1673729480660.png").exact())
                    mouseMove((Location(700, 500)))
                X_Coordinate_Region = Region(105,75,30,14)
                Y_Coordinate_Region = Region(139,77,25,11)
                X_Coordinate = X_Coordinate_Region.text()
                Y_Coordinate = Y_Coordinate_Region.text()
                X_temp = X_Coordinate_Region.text()
                Y_temp = Y_Coordinate_Region.text()
                wait(0.45)
                X_Coordinate_Region = Region(105,75,30,14)
                Y_Coordinate_Region = Region(139,77,25,11)
                X_Coordinate = X_Coordinate_Region.text()
                Y_Coordinate = Y_Coordinate_Region.text()
                if X_temp==X_Coordinate_Region.text() and Y_temp==Y_Coordinate_Region.text() and arrived==False:
                    startMining()
                try:
                    if abs(int(X_Coordinate_Region.text())-1490)<30 and abs(int(Y_Coordinate_Region.text())-540)<30:
                        arrrived=True
                        type("s")
                        mouseMove(mouseMove(Location(0, 500)))
                        wait(0.95)
                        mouseMove((Location(700, 500)))
                        click(Pattern("1673398228807.png").similar(0.50))
                        while(True):
                            if(exists(Pattern("1673729480660.png").exact())):
                                click(Pattern("1673729480660.png").exact())
                                mouseMove((Location(700, 500)))
                            arrived=False
                            try:
                                if abs(int(X_Coordinate_Region.text())-1453)<30 and abs(int(Y_Coordinate_Region.text())-380)<30:
                                    arrrived=True
                                    type("s")
                                    type(" ")
                                    break
                            except:
                              continue
                except:
                    continue
                with open("C:\Users\PC\Desktop\skx\logs.txt", "a") as f:
                    f.write("\nX:" +X_Coordinate+" Y: "+Y_Coordinate+"\n")
                wait(0.5)
    except:
        mouseMove(mouseMove(Location(0, 500)))
        wait(0.4)
        mouseMove((Location(700, 500)))
        startMining()

错误信息

[error] script [ main ] stopped with error in line 13
[error] NameError ( global name 'startMining' is not defined )
[error] --- Traceback --- error source first
line: module ( function ) statement 
9: checkInventoryStatus (  checkInventoryStatus )     startMining()
8: main (  startApp )     checkInventoryStatus()
13: main (  <module> )     startApp()
[error] --- Traceback --- end --------------

解决方案

你遇到的是典型的Python循环导入问题:startMining.py导入了checkInventoryStatus.py,同时checkInventoryStatus.py又导入了startMining.py,导致模块加载时未完全初始化,函数无法被正确识别。

方法1:移除不必要的循环导入

观察startMining.py的代码,里面并没有调用checkInventoryStatus()函数,直接删除startMining.py顶部的from checkInventoryStatus import *即可。这样checkInventoryStatus.py导入startMining.py时,模块能正常初始化,startMining()函数可被正确调用。

方法2:将导入移到函数内部(若确实需要互相调用)

如果两个模块必须互相依赖,可把导入语句移到函数内部,避免模块加载时的循环冲突:

from sikuli import *
from watchMining import *

def checkInventoryStatus():
    from startMining import startMining  # 仅在函数调用时导入
    if(exists("1673723539914.png")):
        if(exists(Pattern("1673723411692.png").exact())):
            startMining()

方法3:优化模块结构

重新规划模块职责,把互相依赖的函数提取到新的公共模块中,从根源上避免循环导入。

内容的提问来源于stack exchange,提问作者Arincovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:21:36