Import for existing scripts doesn't work until re-opening Sikuli

Asked by Tedi Roca

Steps to reproduce:
1. Create a library file containing scripts like:
    def launchApp (self):
     openApp ("c:\\libs\\launch_app_BE.bat")
     openApp ("c:\\libs\\launch_app_FE.bat")

2. Call this script from another file:
    launchApp (None)

Result: "NameError: name 'launchApp' is not defined"

Now close Sikuli and open it again and re-execute the steps described above -> Result:

Environment:
Sikuli X-1.0rc1
Windows XP SP3

Question information

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

This question was originally filed as bug #715799.

This question was reopened

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

let's assume, this is in a myLib.sikuli

def launchApp (self):
     openApp ("c:\\libs\\launch_app_BE.bat")
     openApp ("c:\\libs\\launch_app_FE.bat")

Now you open a new tab in Sikuli IDE and type:

launchApp(None)

This will never work, since called def()'s have either to be in the same file or they have to be imported before, using
import myLib
myLib.launchApp(None)

or
from myLib import *
launchApp(None)

If you want to import other .sikuli to use the contained functions: read the HowTo at: http://sikuli.org/docx/globals.html#importing-other-sikuli-scripts-reuse-code-and-images

Revision history for this message
Tedi Roca (tedi-roca) said :
#2

This is the header of the file calling the script:

############ Imports ############
myScriptPath = "C:\\libs\\"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)

import App_Sanity_Test_library
from App_Sanity_Test_library import *
############ / Imports ############

TestSuite = "GUI_01" # Define test suite name

launchApp (None)

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

then I suppose, that directory c:\libs contains a Sikuli named "App_Sanity_Test_library.sikuli" and this in turn contains the def

def launchApp (self):
     openApp ("c:\\libs\\launch_app_BE.bat")
     openApp ("c:\\libs\\launch_app_FE.bat")

If this is the case, then this should principally work.

But I am not really sure, wether .bat files can be used with openApp(), I remember, it has to be an .exe.
If I am right (cannot test it today - no Windows available): try to openApp("cmd.exe") and paste your command and type(Key.ENTER).

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

not a bug

Revision history for this message
Tedi Roca (tedi-roca) said :
#5

RaiMan, the point is that if I try running it as exposed above it doesn't work. But just exiting Sikuli and opening it again, works perfectly.

Revision history for this message
Tedi Roca (tedi-roca) said :
#6

RaiMan, the point is that if I try running it as exposed above it doesn't work. But just exiting Sikuli and opening it again, works perfectly.

Revision history for this message
Tedi Roca (tedi-roca) said :
#7

RaiMan, the point is that if I try running it as exposed above it doesn't work. But just exiting Sikuli and opening it again, works perfectly.

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

ok, then make the following simple test:

-- first .sikuli name it: theLib.sikuli with content:
print "[Info1] this should only be printed once at start up"
def theFunction():
   print "[Info2] Hello, should be printed every time you call me"

-- second .sikuli name it: theMain.sikuli
import os.path
dir = os.path.dirname(getBundlePath())
if not dir in sys.path: sys.path.append(dir)
from theLib import *
for i in range(3): theFunction()

save both .sikuli in the same directory of your choice.
Restart the IDE.
Run the script theMain.Sikuli in the IDE.
On Rerun, message [Info1] should not come any more, since it only is processed at import.

Revision history for this message
Tedi Roca (tedi-roca) said :
#9

That's right. But is there any way to process the imported library again without having to reopen Sikuli?

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

principally Python delivers all the means, to reset your imports:
You have to delete all your imported modules from sys.modules. I have only seen that some time ago and never tested it. Especially I do not know, whether you have to deal with the imported names, when using from x import *.

Since I am developing more complex scenarios in NetBeans, I do not have this problem at all. Same is true, if you run your main script from commandline.

Can you help with this problem?

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

To post a message you must log in.