JSR223 and Jython module import

I’m curently converting my rules to jython. I use a file to store variables that are used across several jython scripts. Said file is imported in all scripts.

Today I found out that files imported seem to be somehow cached even across changes to the source. When I change the imported file a restart of openhab is needed to get JSR223 to pick up the changes.

Has anybody else observed such behaviour and this is a “know feature”? Is there any way to clear the cache apart from reloading openhab?

That’s a Python feature. Modules imported into a Python interpreter are cached and shared across the modules doing the import.

As a simple overview… when you do an import, Python checks its module cache (sys.modules). If the module is there it just sets an attribute in the importing module to reference the cached module. If the imported module is not in the cache, Python loads the module, caches it and then sets the attribute in the importing module.

You can explicitly force a Python module reload with the reload builtin function.


For example,

import mymodule # find or load mymodule, set 'mymodule' to reference it
reload(mymodule) # forces re-import of mymodule

Note that the reloading behavior of rule scripts in the script directory is an openHAB feature and not the same as the Python import mechanism.

1 Like

@steve1 Thanks for your explanation.