Importing Sikuli Script

Asked by Harry Readinger

I am attempting to use a Sikuli Script as a module for other scripts in my automation suite (its actually launching the program, making it a module makes me not have to copy/paste it at the beginning of each script, and also makes the scripts independent, as they don't have to rely on the program already being open) Here is some code:

Script I'm Importing (importLogin.sikuli)

from sikuli import *
import sys
#this is/was a test function (duh) to quickly determine if the import "worked"
def test():
    popup("works!")

def launch():
    App.open("C:\Path\to\my\program")

Script that I'm importing to:

import loginImport

importPath = "C:\\path\\to\\my\\imports"
if not importPath in sys.path: sys.path.append(importPath)

loginImport.test()

This throws a "NameError: global name 'popup' is not defined"

I feel like i have (but probably haven't) done everything required as laid out in the Sikuli Documentation: http://sikuli.org/docx/globals.html#importing-other-sikuli-scripts-reuse-code-and-images

Question information

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

It has to be:

from sikuli.Sikuli import *

in the imported module.

Revision history for this message
Tsung-Hsiang Chang (vgod) said :
#2

from sikuli import * should work, too. If not, it's a bug.

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

@Sean
Sorry, forgot, that this was changed. And yes, it works ;-)

@Harry
Before working with different directories, you should make a test with main.sikuli and loginImport.sikuli in the same directory, since this is supported in rc3 without managing sys.path.

So this should work:

#content main.sikuli
import loginImport
loginImport.test()

#content loginImport.sikuli
from sikuli import *
def test():
    popup("works!")

--- If you then decide to move your sub modules to another directory:
first you have to arrange sys.path before importing.

importPath = r"C:\path\to\my\imports"
if not importPath in sys.path: sys.path.append(importPath)

import loginImport

--- double backslash
if you use the raw string r"contains \ and more", you do not need to doublee the backslashes
so it should be: App.open(r"C:\Path\to\my\program")
restriction: a \ cannot be the last character in the string

Revision history for this message
Harry Readinger (temporary22) said :
#4

@RaiMan, @Tsung

Thanks for the help guys, got it working!