How to loop a master script (one that runs other scripts)

Asked by TC

So I have 2 scripts (it was one before, but I had to break it up because it was too long). Call them script1.sikuli and script2.sikuli

And I have a master script that runs them. The script1.sikuli and script2.sikuli are inside the master.sikuli folder. (I tried it when master, script1, script 2 were all in a single folder, same thing happens).

So in master.sikuli I have written down:

for n in range (3):
    import script1
    import script2

When I run though, it goes through both scripts perfectly, but it stops after only one run.

Any way to make this happen?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

  • by TC
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Python import only runs the imported script at first time, to learn about the names defined in the script. Import is usually used to make variables, functions and classes available in other scripts (DRY principle, reuse what already works).

So the most Python-like way would be to wrap the code in the scripts into a def() :

--- script 1
def script():
    # existing code indented one level

--- script 2
def script():
    # existing code indented one level

--- main script
import script1
import script2
for n in range (3):
    script1.script()
    script2.script()

without any changes you can do this "ugly hack":

for n in range (3):
    if n == 1:
        import script1
        import script2
        continue
    reload(script1)
    reload(script2)

Revision history for this message
TC (tvz888) said :
#2

Interesting for the first one, once you run the master script, the sub-scripts seem "locked" and unable to edit (until restarting sikuli)

But it is fine, the ugly hack works great.

Thanks for the answer, I have no computer science background and your other answers always come up on google searches and it's helped me tremendously :)

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

--- the sub-scripts seem "locked"
Since in the same session of the IDE, imports once done are not refreshed on rerun of the masterscript, any edits in imported scripts in the same IDE session are only reflected after restart of the IDE.

With version 1.1.1 the first one will work, since imported SikuliX scripts are internally refreshed on rerun. But this would only help, if you switch to solution 1.

... and thanks for kind feedback.
Have fun.

Revision history for this message
TC (tvz888) said :
#4

Using the ugly hack (written like your earlier post), I sometimes get

[error] script [ mainscript] stopped with error in line 6
[error] NameError ( name 'subscript' is not defined )

It definitely works sometimes. Sometimes when I run the sub-scripts individually, and then run the main script, it works, as if it warmed up the main script. It used to work right when I opened Sikuli. Not sure what is going on.

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

Then you still have some naming conflicts.

As mentioned: If you proceed to use this solution instead of switching to being pythonic, then you have to take care for having a strict naming convention, that does not lead to name clashes.

Can you help with this problem?

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

To post a message you must log in.