Unable to call SikuliX 1.1.2 script by import function

Asked by sami baig on 2018-01-21

I am using below script to call another SikuliX script but it is not working and giving following error in line 5.

SikuliX Script:
from sikuli import *
myScriptPath = "C:\Users\Sami\Scripts\test.sikuli"
addImportPath(myScriptPath)
import test

SikuliX Error:
[error] script [ Import ] stopped with error in line 5
[error] NameError ( !!WHILE IMPORTING!! name 'find' is not defined )
[error] --- Traceback --- error source first line: module ( function ) statement 1: test ( <module> ) find("1514113260953.png")
[error] --- Traceback --- end --------------

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2018-01-31
Last reply:
2018-02-01
sami baig (gabbarmic) said : #2

According to given introductions in this link (http://sikulix-2014.readthedocs.io/en/latest/scripting.html#importing-other-sikuli-scripts-reuse-code-and-images), I have set sys.path but still it is giving same error as above:

SikuliX script:

from sikuli import *
import sys
#sys.path.append("C:\Users\Sami\Scripts\test.sikuli")
myScriptPath = "C:\Users\Sami\Scripts\test.sikuli"
addImportPath(myScriptPath)
import test

RaiMan (raimund-hocke) said : #3

Sorry, but that is not what is suggested there.

If the main and imported script are in the same folder (a SikuliX script is the someScript.sikuli and not the included someScript.py!!!) there is no need to use sys.path.

# main as main.sikuli
import test

# sub in same folder
# as test.sikuli
from sikuli import * # not needed in main!!
... code

Be sure you have understood and checked all options mentioned in
 (http://sikulix-2014.readthedocs.io/en/latest/scripting.html#importing-other-sikuli-scripts-reuse-code-and-images)

sami baig (gabbarmic) said : #4

Madarchot :)

RaiMan (raimund-hocke) said : #5

???????????????????????????????

I am German

sami baig (gabbarmic) said : #6

I think this codes not working because I am using 1.1.2 version of SikuliX and it is stated 1.1.1 on webpage ? Please help me out, I have also tried basic example as well but not succeed.

SikuliX script:

myScriptPath = "C:\Users\Sami\Scripts\test.sikuli"
addImportPath(myScriptPath)
import test

RaiMan (raimund-hocke) said : #7

Go back to comment #3 and try to understand

sami baig (gabbarmic) said : #8

Give me a hint, should I have to set "Environment Variables" for "sys.path" ?

RaiMan (raimund-hocke) said : #9

Everything needed is in comment #3

sami baig (gabbarmic) said : #10

Now, I have understand what you told and I have saved myscript.sikuli in the folder of main script and using below code:

myScriptPath = "C:\Users\Sami\Scripts\Import.sikuli"
addImportPath(myScriptPath)

import test
test.script01(event)

But still it is giving below error: :(

[error] script [ Import ] stopped with error in line 9
[error] NameError ( !!WHILE IMPORTING!! name 'Env' is not defined )
[error] --- Traceback --- error source first line: module ( function ) statement 8: test ( <module> ) Env.addHotkey("n", KeyModifier.ALT + KeyModifier.CTRL, script01)
[error] --- Traceback --- end --------------

RaiMan (raimund-hocke) said : #11

No, you still do not understand the SikuliX import concept :-(

When talking about a SikuliX script, THE FOLDER of the script is meant!

so a setup might be:

-- someFolder
   -- main.sikuli
   -- sub.sikuli

in this setup

# main.sikuli
import sub
sub.test()

# sub.sikuli
from sikuli import *
def test():
  pass # do something

... no need for any path's or addImportPath

BTW: (basic Python knowledge ;-) backslashes in strings must be doubled or the string must be mentioned as raw string
"C:\\Users\\Sami\\Scripts\\Import.sikuli"
or
r"C:\Users\Sami\Scripts\Import.sikuli"

Can you help with this problem?

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

To post a message you must log in.