calling functions between multiple files

Asked by alex

I have 2 files that need to call functions from each other. The problem is that I get an error

NameError: global name 'main' is not defined

my two files are as follows (i have cut out a bunch of stuff to narrow in on the issue)

sarkoth.sikuli

import general
from sikuli import *
from general import *

def main():
    do things
     leave()

    else:
        leave()

runs=0
while runs>=0:
    main()

general.sikuli

from sikuli import *

def leave():
    type("t")
    main()

sarkoth.sikuli is where my script is run from and it will call leave() which is located in my general.sikuli. The leave function executes but then gets hung up when its time for it to call main() from sarkoth.sikuli

i've also tried adding to the general.sikuli but it still doesn't work.

import sarkoth
reload(sarkoth)
from sarkoth import *

anyone have ideas?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

good, that it did not work ;-)

This would give an endless loop, since I cannot see, that recursiveness is implemented in your script.

when running in IDE, this should work:

# sarkoth.sikuli

from sikuli import *
import general # comment 1
reload(general) # comment 1
from general import *

def main():
    do things
     retVal = leave() # comment 3

    else:
        retVal = leave()

runs=0
while runs>=0:
    main()

# general.sikuli

from sikuli import *

def leave():
    type("t")
    return something # comment 2

That's all, if both scripts are in the same folder.
If not, you have to set sys.path accordingly (see docs)

-- comment 1
These 2 statements are only needed, if you run the main script (sarkoth) in the IDE and in parallel make changes to subs (general). So you can have open all scripts in the IDE and make concurrent changes.

-- comment 2
at this point leave() is ended and jumps back to the line after it was called in main(). something might be a return value, that can be checked in main.

-- comment 3
we store the return value of leave(), so we can use it later (e.g. to check,wether we should terminate our script or do any other action.

Revision history for this message
alex (alexander-appugliese) said :
#2

I intend it to run endlessly. I run this script in sikuli x and run it from sikuli so i always use cmd shift c to stop it. This is fine for my uses unless there is a benefit to making it into an exe. However since I am constantly updating my script and making changes i probably won't finalize it into an exe until I'm done making changes.

---comment 2

i understand that when leave() is done running it will continue to the next line in main but the reason i am calling it again is so that it will start with the first line in main. Is there any way to do this?

---comment 3

using the retVal could probably add an if statement like the following

if retVal = 10:
     main()
else
     continue with program

while this would probably accomplish the same thing i feel like its unnecessary and there might be a simpler way.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Running things endlessly is done using a loop, that never ends and has to be aborted from extern:

while True:
    print "I run endlessly"
    wait(1)

After waiting 1 second it prints again - endlessly.

So with your loop

while runs >= 0:
    main()

you already have your endless repeating of main(): every time you reach the last line in main(), main is ended and started all over again.

But with your construct, you will
- start with main()
- call leave
- which calls main()
- which calls leave()
- which calls main()
- ....
and none of these functions will ever be ended, which will lead to an "out of memory" exception after some time.

So the conclusion:
- to repeat something (even endlessly) we have the looping constructs while and for
- recursive programming (a function calls itself or a function that called it) is a tool to solve complex problems that might iterate many times, but come to an end by itself. These kind of workflows we script with Sikuli do not have these characteristics.

Can you help with this problem?

Provide an answer of your own, or ask alex for more information if necessary.

To post a message you must log in.