[SOLVED]Where to put own Jython Modules

Just making my first step in Jython:

Where do I put library modules which can be imported by other modules?

I have created in personal:

myLib.py

def myLog(msg):
    from core.log import logging, LOG_PREFIX
    log = logging.getLogger(LOG_PREFIX + ".this.is.my.log")
    log.info(msg)

test.py

from myLib import  myLog

myLog("Hello World!!!")

I get following error

2019-05-15 19:01:07.297 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'personal/test.py'
2019-05-15 19:01:07.532 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/personal/test.py': ImportError: No module named myLib in <script> at line number 2
2019-05-15 19:01:07.534 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: personal/test.py

putting the file in “lib/personal”
image

and refencing:

from  personal.myLib import  myLog

myLog("Hello World")

3 Likes

If you’re more comfortable with using LogAction (what is used in the rules DSL), you can also do this…

from org.eclipse.smarthome.model.script.actions.LogAction import logInfo, logDebug
test = 55555
logInfo("Rules", "This is a test [{}]", test)
logDebug("Rules", "This is a another test [{}]", test)
1 Like

Was just a test how to write library modules.
No practical use.

1 Like

Thank you, @lukics! I found this by searching, saved me making my own post. :slight_smile:

I’ll add instruction to the docs for personal scripts and packages.

2 Likes