Organizing sikuli scripts into packages.

Asked by Dillon on 2019-01-08

I'm building an automation framework for an application and am attempting to create packages with sikuli scripts with similar structures to normal python packages.

Example:

/modules
    /application
        /__init__.py
        /functions.sikuli
            /functions.py
    /windows
        /__init__.py
        /functions.sikuli
            /functions.py

The problem is __init__.py doesn't recognize the .sikuli files (folders), therefore they don't get added to the package.

I can try to cheat my way around this by adding the following to the __init__.py file but this causes some other issues where the sikuli modules look in the wrong place for images.

# code

import sys
import os

# Get the base path.
base_path = os.path.dirname(os.path.realpath(__file__))

sys.path.append(os.path.join(os.path.join(base_path, "functions.sikuli")))
import functions

# end

Does Sikuli support package structures similar to python?

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Dillon
Solved:
2019-01-10
Last query:
2019-01-10
Last reply:
2019-01-09
RaiMan (raimund-hocke) said : #1

As long as SikuliX works with .sikuli folders, that contain the .py script file and the images referenced in that script, there will not be an easy solution for that.

Your workaround can be made simpler, since the import in a SikuliX script:
(see https://sikulix-2014.readthedocs.io/en/latest/scripting.html#importing-other-sikuli-scripts-reuse-code-and-images)
- looks for the script in the folder, the importing script folder is located
- auto-adds the imported .sikuli to the sys.path
- auto-adds the imported .sikuli folder to the imagepath

Be aware:
since all images are looked-up in the imagepath only with their name (without path), you need a naming convention, that avoids name clashes between different scripts.

The only way to just use the plain Python module structure is to separate code and images. As a consequence you only can use the SikuliX IDE for capturing/storing your images and have to use another editor/IDE for your .py files (e.g. IntelliJ's PyCharm).
The one or more image-only .sikuli have to be added to the imagepath when needed.

I have on the list, to support such module structures in the SikuliX IDE.
But this does not help you currently ;-) and will surely take a few months to get ready.

Come back if you run into problems.
In such cases you might contact me directly and send your trials to my mail: sikulix---at---outlook---dot---com.

Dillon (dillonm197) said : #2

Thank you very much for the quick reply RaiMan. Your hard work is greatly appreciated. I thought I'd share that I successful in my attempts of getting python to recognize the modules within the seperate sikuli bundles ny adding each sikuli bundle to the __path__ within each __init__.py file. Maybe this should be added to the documentation?

# __init__.py

from sikuli import *

import os

# Get the base path.
base_path = os.path.dirname(os.path.realpath(__file__))

# Since python packages only recognize .py files
# in the same directory, we need to add the .sikuli
# folders to the __path__. This will add the .py
# files inside them to the package so that we can
# reference them directly.
for dir in os.listdir(base_path):
    if ".sikuli" in dir:
        path = os.path.join(base_path, dir)
        __path__.append(path)
        addImagePath(path)