Cannot use my pyhton function on a referenced .py file

56 views Asked by At

I just started on Python, and still trying to learn it. I recently started learning Sikulix and its applications. Would highly appreciate if you could share your ideas...

Problem is that I have 3 py files. Say:

main.py

checkInventory.py

startMining.py

I only defined 1 function on checkInventory.py, startMining.py each, functions are also named after the file names (checkInventory(), startMining() respectively)

When I run the main.py it is calling checkInventory() using:

beginning of main.py...

from checkInventoryStatus import *

No problem on this. I can call checkInventoryStatus() function with no problem on main.py.

However, checkInventory() also runs startMining() at some point and it keeps giving this error:

NameError ( global name 'startMining' is not defined )

I am also importing all the content of startMining.py to checkInventory.py using:

beginning of checkInventory.py...

from startMining import *

I tried to import all elements of startMining.py to checkInventory.py but it is failing to call the function startMining() from checkInventory.py.

Main

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

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

Here is the error message:

[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 --------------
1

There are 1 answers

0
onlyme On

I believe it is due to "circular import" behavior between checkInventoryStatus.py and startMining.py.

You can structure your code in a way that eliminates calling modules in a circular motion. For example, you can move checkInventoryStatus() into startMining.py which will kill the dependency to checkInventoryStatus.py (assuming that all you code consists of only these three scripts.)