how to import all the sikuli files in one directory (myLib folder)

Asked by Pablo Frank

I tried to implement the "Another example: Importing from the same directory" in page
"Global Functions and Features".

Looking in the forum i couldn't find solution: these answers don't help:

136832 Calling a common Function Library
138472 Trying to use system path for sikuli script and import it in other scripts.

and reading more answers just got more confused.

One of my tries are below:

"myLib.Sikuli" folder and "ImportMyLib.sikuli" file in C:\Program Files\Sikuli

"ImportMyLib.sikuli" content:

import os
# get the directory containing your running .sikuli
myPath = os.path.dirname(getBundlePath())
if not myPath in sys.path: sys.path.append(myPath)
# now you can import every .sikuli in the same directory
import myLib

-------------------------------------------------------------------------
when ImportMyLib.sikuli run, the message displayed is:

[error] Stopped [error] An error occurs at line 8 [error] Error message: Traceback (most recent call last): File "C:\Users\Pablo1\AppData\Local\Temp\sikuli-tmp406801296734407928.py", line 8, in import myLib File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\sikuli\SikuliImporter.py", line 42, in load_module File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\sikuli\SikuliImporter.py", line 22, in _load_module ImportError: No module named myLib

thanks in advance for any help

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
eliteSKL
Solved:
Last query:
Last reply:
Revision history for this message
Best eliteSKL (camaro70s) said :
#1

"ImportError: No module named myLib" double check the name of the file and location. make sure your import statements are after the myPath part

okay i have files in this folder:
c:\ABunchOfSikuliFiles
        scriptA.sikuli
        scriptB.sikuli
        scriptC.sikuli

If i want those to import correctly i will need this line of code at the top of those files --> from sikuli.Sikuli import *

now i make my new file that is doing to do some importing.

c:\ABunchOfSikuliFiles
        MyNewAwsomeScript.sikuli
        scriptA.sikuli
        scriptB.sikuli
        scriptC.sikuli

the lines of code at the top of MyNewAwsomeScript.sikuli are:

import os

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

import scriptA.sikuli
import scriptB.sikuli
import scriptC.sikuli

scriptA.afunctionFromhere()
scriptB.AfunCtionfromHERE()

Revision history for this message
eliteSKL (camaro70s) said :
#2

only important if you want your script to be portable. other wise you could put your scripts all over your hard drive and it would be fine. just look at the documentation it has both examples.

http://sikuli.org/docx/globals.html#importing-other-sikuli-scripts-reuse-code-and-images

Revision history for this message
Pablo Frank (frank-pablo) said :
#3

Thanks eliteSKL, that solved my question.