help importing modules

Asked by rob

im trying to modularize a macro i use at work but i cant figure out how to import the functions. i use windows 7 in the office and windows 8 at home and the idea is to have a script(s) i can zip up and share easily. for the moment i have a test folder set up like this:
\test_folder
    -functions.sikuli
    -test.sikuli
the test_folder contains only the two .sikuli folders
the code in functions.sikuli looks like this:

def rtb(): #test function
    r = Region(4,106,88,100)
    r.hover("1355538617821.png")
    return

the code in test.sikuli looks like this:

import os
myPath = os.path.dirname(getBundlePath())
if not myPath in sys.path: sys.path.append(myPath)
import functions
rtb()

and i get this error message when i run test.sikuli from the IDE:

[error] Stopped
[error] An error occurs at line 5
[error] Error message: Traceback (most recent call last):
 File "C:\Users\robert\AppData\Local\Temp\sikuli-tmp6921701362547521901.py", line 5, in
 rtb()
NameError: name 'rtb' is not defined

i read the faqs and some questions answered on the forums and i tried importing the functions a few other ways but im stuck. any help would be appreciated.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
j
Solved:
Last query:
Last reply:
Revision history for this message
j (j-the-k) said :
#1

You have to change test.sikuli:

Either write "from functions import *" instead of "import functions" to import all elements of the functions module into your test.sikuli. However this could produce conflicts if you have defined any method or variable with the same name in functions.sikuli as in test.sikuli.
So it would probably be better just to write "functions.rtb()" instead only "rtb()".

Revision history for this message
j (j-the-k) said :
#2

Just an addition for your understanding:

You may notice the similarity to the other module you imported:

import os

To use the path attribute of the os module, you have to write os.path.
It's the same with your own imported modules.

Revision history for this message
rob (reg82) said :
#3

thanks for your help. i think it worked, but im getting a new error:

[error] Stopped
[error] An error occurs at line 6
[error] Error message: Traceback (most recent call last):
 File "C:\Users\robert\AppData\Local\Temp\sikuli-tmp8271075017912304850.py", line 6, in
 functions.testing()
 File "C:\Users\robert\Documents\python\script_library\not_entered\functions.sikuli\functions.py", line 6, in testing
 r = Region(4,106,88,100)
NameError: global name 'Region' is not defined

i really dont understand this because region should definitely be defined. i mean its a sikuli class right? or does this have something to do with importing?

Revision history for this message
Best j (j-the-k) said :
#4

All sikuli Classes are only automatically available in the module you are launching with sikuli, not the ones you're importing into that one.
You have to add
from sikuli.Sikuli import *
on top of you functions.sikuli.

Revision history for this message
rob (reg82) said :
#5

Thanks j, that solved my question.