0

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 --------------
Arincovic
  • 1
  • 1
  • Show us the full code. Don't make us guess what is in each file. – John Gordon Jan 14 '23 at 23:53
  • Just added, sorry, still trying to understand how to use stackoverflow :( – Arincovic Jan 15 '23 at 00:00
  • You still haven't shown us all the code (where is `startMining.py`), and you haven't shown us the full error message. – John Gordon Jan 15 '23 at 00:14
  • Just did now. ty. – Arincovic Jan 15 '23 at 00:21
  • 1
    I believe the problem is that `checkInventoryStatus.py` imports `startMining.py`, and also vice-versa `startMining.py` imports `checkInventoryStatus.py`. That's a _circular import_, and you can't do that. – John Gordon Jan 15 '23 at 00:24
  • You'll need to rearrange your modules and functions so this doesn't happen. Imagine that your code is structured like a layer cake, and a module can only import from lower layers, not same or higher layers. (And by "layer cake" I don't mean directory structure; I just mean that as a general notion.) – John Gordon Jan 15 '23 at 00:41

1 Answers1

0

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

onlyme
  • 1
  • 2