Script runs twice when it is imported

Asked by P. P.

Hello,

I have a main script with the following structure:

# script1
import Script1
reload (Script1)

# script2
import Script
reload (Script2)

.
.
.

# scriptN
import ScriptN
reload (ScriptN)

When I run this main, some scripts run twice.
What can I do to avoid that?

Thanks!

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
RaiMan (raimund-hocke) said :
#1

if your imported modul has runnable code on indent level null (not in def()'s nor in classes), then this code is run at import AND reload.

Generally an imported module should only have code, that only does something when using a name defined in the module (e.g. calling a function or using a variable that has its content defined in the module).

Hence for the average SikuliX scripts everything in a module should be packed into def()'s.

If you want to run code by importing it, the feature runscript is much better.
BTW: with version 1.1.1 the reload for .sikuli is done internally automatically (not needed anymore).

Revision history for this message
P. P. (ppg7) said :
#2

Thanks for the answer, RaiMan.

If I use runScript feature, should I use def()'s?
What is the recommendation?

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

--- If I use runScript feature, should I use def()'s?
... definitely ;-) not, since this exactly is the difference between running a script and "running" something via import.

Revision history for this message
P. P. (ppg7) said :
#4

Thanks RaiMan, that solved my question.