python 无法对引用的.py文件使用pyhton函数

djmepvbi  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(152)

我刚开始学习Python,还在努力学习。我最近开始学习Sikulix和它的应用程序。如果你能分享你的想法,我将非常感激...
问题是我有3个py文件。
main.py
checkInventory.py
startMining.py
我只在www.example.com和www.example.com上分别定义了1个函数checkInventory.pystartMining.py,函数也以文件名命名(分别为checkInventory()和startMining())
当我运行www.example.com时main.py,它使用以下命令调用checkInventory():
main.py的开头...
from checkInventoryStatus import *
这没有问题。我可以在main.py上调用checkInventoryStatus()函数,没有问题。
然而,checkInventory()也会在某个时候运行startMining(),并且它会不断地给出以下错误:
名称错误(未定义全局名称“startMining”)
我还使用以下命令将www.example.com的所有内容导入startMining.py到checkInventory.py:
开始检查库存. py ...
from startMining import *

我尝试将www.example.com的所有元素startMining.py导入checkInventory.py,但无法从checkInventory.py调用函数startMining()。

主要

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()

检查库存状态

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
                #f.write("\nX:" +X_Coordinate+" Y: "+Y_Coordinate+"\n")
                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 --------------
o4tp2gmn

o4tp2gmn1#

我认为这是由于checkInventoryStatus.pystartMining.py之间的“循环导入”行为。
你可以用一种避免循环调用模块的方式来组织你的代码,例如,你可以把checkInventoryStatus()移到startMining.py中,这样就消除了对checkInventoryStatus.py的依赖(假设你所有的代码都是由这三个脚本组成的)。

相关问题