How to import Sikuli file but not run it immediately?

Asked by Test

Example:

import module
<program does stuff>
reload(module) (within a function, FYI)

When I run the program for the FIRST time after launching the Sikuli IDE, the imported Sikuli file runs once through, then my program does stuff, then the imported file runs again because of the reload. But what I want is for it NOT to run through once upon import, but rather only when I reload it.

How can I achieve that? Thanks in advance.

Question information

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

by design, import is not intended to use it to run the contained script code completely.
imported modules usually contain stuff that is used in the main workflow by calling methods/functions and using classes, that are defined/setup in the module.

In Sikuli though it might be tempting, to use it for running scripts, since you get the image path management on top.

Since any solution for your problem would need to put the script content into some <if condition: > construct to manage run/do-not-run, you should make the right step:

--- put the complete content of the script inside your imported module into a <def mySubScript(): > construct

and the use the module as intended by import:

import module # nothing happens here now
module.mySubScript() # will run your stuff

now you have complete control and even more options like adding parameters to modify a run of module.

HowTo:

# current content of your module
statement
statement
...
statement

# modified with function
def mySubScript():
    statement
    statement
    ...
    statement

--- below the def statement your complete code is simply intended one level (see faq 1800)

Revision history for this message
Test (c4456517) said :
#2

Thanks very much for the quick reply RaiMan, this workaround does work well.

Revision history for this message
Test (c4456517) said :
#4

Point to note too:

I still had to reload the module within that function.

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

should not be necessary.

Why do you think, you need it?

Revision history for this message
Test (c4456517) said :
#6

You're right - must have been some kind of odd glitch yesterday.

When I attempted to run the function, nothing happened, so I put in the reload(module) code and it worked.

Today, it works fine without the reload. Odd.

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

Then it is no magic, I guess:

When working in the IDE, an imported module is only loaded the first time the script is run. On a rerun, the import simply does nothing, because in the IDE the internal state is not reset with respect to imports.

Therefor if you change the imported module after it was imported and rerun the main script, the changes are not reflected.

The solution is to either stop and restart the IDE or to use
import module
reload(module)

which is the recommended solution when working in the IDE while working on the module and testing it.

The docs know more: http://doc.sikuli.org/globals.html#importing-other-sikuli-scripts-reuse-code-and-images

Revision history for this message
Vinodh Yelugoti (vinodh-yv123) said :
#8

Hello Raiman -

I have a probelm with reload function! It starts executing complete module.

for eg:

#Script1

from sikuli import *
def function1():
..
function1()

def function2()
..
funtion2()

#In script 2

import script1
reload(script1)

After this, it starts executing complete script1 i.e function1 and function2!

What should i do if i want to execute only function2 of script1 in script2?? But i also need dynamic changes of screen captures that i do in script1 to be reflected in script2

Thanks,
VINODH

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

@vinodh
see your own question