[solved] Calling python scripts from rules - imports not found?


I am trying to call a python script from an OpenHAB rule. My problem now is that if the script is executed from a rule, it does not find its imports:

File “/home/openhabian/leaf-python-mqtt/leaf-python-mqtt-1.py”, line 6, in

import pycarwings2

ImportError: No module named pycarwings2

When started manually from a putty window (logged in with the openhabian user), the script works. The script is located in /home/openhabian/.local/lib/python2.7/site-packages/pycarwings2.
Any idea why this happens ? I assume OpenHAB is running under the openhabian user as well and should have the same sys.path ?

I am running OpenHAB 2.5.9 on a Raspberry Pi4, openhabian, minimal install.

Many thanks

Are you sure it is running the same Python version in the same Python virtual env? Python 3 is the default now on many OSs since Python 2 is no longer supported by its developers.

I am starting to use @Spaceman_Spiff’s HABApp & Pythion 3 with my rules.

the pycarwings2 library is installed both in python 2 and 3, so that should not be the reason (if I get your answer right). There must be a difference in the ‘environment’ that OpenHAB uses and that console windows use. But I have no idea why …
Did you install any addons to be able to call python scripts ?
BR, Brandy

HABApp is a separate daemon that uses the REST API to communicate with openHAB



problem solved, apparently OpenHAB is not running under the openhabian user, the external libraries have to be installed using ‘sudo pip …’ for all users.


1 Like

If you enjoy using Python and native rule engines, you might want to look into the new rule engine. which includes scripted automation. This rule engine was labelled the Next Generation Rule Engine and marked as experimental in OH2, but it is now the only rule engine officially available in OH3. With scripted automation, you can use Jython and the helper libraries, which can use directly use Java, OH classes and services, and the OH automation API, rather than using the REST API (slower and limited functionality).