How to call a function from other module

Asked by Rathinavel

Hi am new to sikuli. Please give the solution how to solve the below problem:

Am having 2 sikuli scripts.
Expectation: => Script1 and 2 should run individually.(Script2 working fine, but not Script1)

----------------------------------------------------------------
Script1.sikuli:

import Script2
from Script2 import function2

popup("hello, I am script1")
Script2.function2()

Output : hello, I am script2, function1
                 hello, I am script2, function2
                 hello, I am script1

Not meeting my expectation.My Expectation is
hello, I am script1
hello, I am script2, function2
----------------------------------------------------------------
Script2.sikuli:

from sikuli import *

def function1(): #
    popup("hello, I am script2, function1")

def function2():
    popup("hello, I am script2, function2")

function1()
function2()

Output : hello, I am script2, function1
                 hello, I am script2, function2
Meeting my expectation.
---------------------------------------------------------------

Question information

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

script code at indent level 0 (function1()
function2() in script2 in this case) is executed at import time.

so script2 should only be:
from sikuli import *

def function1(): #
    popup("hello, I am script2, function1")

def function2():
    popup("hello, I am script2, function2")

... and script1 can either be
import Script2
popup("hello, I am script1")
Script2.function2()

... or
... and script1 can either be
from Script2 import *
popup("hello, I am script1")
function2()

... in the second case it might come to name clashes.

Revision history for this message
Rathinavel (ratnasatya) said :
#2

Hi RaiMan,

Thanks for the reply.
If I changed the Script2 as per the suggestion, I will not get any output. Script2 will act as a library file....

For me, Script2 also should work, if I run Script2 alone individually. Correct me if am Wrong

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

I always recommend, to strictly differentiate between library scripts (no executable code (indent level 0)) and execution scripts.

Library scripts contain classes and functions, that are used elsewhere more than once.

An execution script represents a workflow, where one or more classes/functions are used from imported library scripts.

As said - my recommendation based on experience.

But if you really want to: level 0 code in library scripts should be guarded with an if-statement, that only gets true if the library script is run as main script.

The most often used snippet for this case is
if __name__ == "__main__":
    # here your code, that only runs, if the module is a main script (not imported)

Revision history for this message
Rathinavel (ratnasatya) said :
#4

Thanks RaiMan !