How to use functions from the main script?

Asked by Vit on 2017-11-02

Hello! Sorry for my english.

I have 2 files in the same folder: main.sikuli and second.sikuli

# --- main.sikuli:
def test():
    return(123)

import second
reload(second)
from second import *
startSecondScript()

# --- second.sikuli:
from sikuli import *
def startSecondScript():
    import main
    from main import *
    y = test()

Result:
[error] script [ main ] stopped with error in line 7
[error] NameError ( global name 'test' is not defined )

How my second script can use functions from main script?

Thanks in advance!

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2017-11-02
Last reply:
2017-11-03
aravind (aravind981) said : #1

Assuming that you want to call a function present in main.sikuli from second.sikuli , your could should be similar to this

 second.sikuli ::
import main
def startSecondScript():
    y = main.test()
    print(y)
startSecondScript()

main.sikuli ::
from sikuli import *
def test():
    return(123)

Please read the relevant answer in the below link
https://answers.launchpad.net/sikuli/+question/660256

Vit (polet) said : #2

Thanks for your answer! Unfortunately, this does not work (the same error).

RaiMan (raimund-hocke) said : #3

I think this way it makes more sense:

Assuming that you want to call a function present in sub.sikuli from main.sikuli , your solution could be similar to this

sub.sikuli ::
from sikuli import * # needed in imported scripts
def doSomething():
    return(123) # what should be done in main.test

main.sikuli ::
import sub
def test():
    sub.doSomething()
test() # should print 123

take care that main.sikuli and sub.sikuli are in the same folder

Can you help with this problem?

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

To post a message you must log in.