Python issue with exec binding "ModuleNotFoundError:" even though module is installed and working

Getting an error while trying to execute a python script with the exec binding:

2022-10-23 21:48:14.683 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘change_shower_hum-1’ failed: For input string: “Traceback(mostrecentcalllast):
File”/etc/openhab/scripts/hum_shower.py",line4,in
importAdafruit_DHT
ModuleNotFoundError:Nomodulenamed’Adafruit_DHT’
Traceback(mostrecentcalllast):
File"/etc/openhab/scripts/hum_shower.py",line4,in
importAdafruit_DHT
ModuleNotFoundError:Nomodulenamed’Adafruit_DHT’" in change_shower_hum

obviously this is telling me that when executing the python script hum_shower.py that it is not finding the DHT module.

here is my thing:

Thing exec:command:hum_shower [command=“python3 /etc/openhab/scripts/hum_shower.py”, interval=15, autorun=true]

So i can run this command manually in terminal, and it works fine.

Unless, I try to run it as

python /etc/openhab/scripts/hum_shower.py

then I get the same missing module error.

So apparently even though I have stated the script run in python 3 , It still seems to want to run it on the older python (2.7)

Not sure whey this would be happening.

PS> this is a recent upgrade from OH 2 to OH 3. Everything was functioning without issue over on OH 2.

any help or points in what might be happening, would be greatly appreaciated+++

PS> here is the python script:

#!/usr/bin/env python3

import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22

pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

temperature = 9/5.0 * temperature + 32

if humidity is not None and temperature is not None:

xyzzy = (‘{0:0.2f}’.format(temperature))
xyzzyy = int(float(xyzzy))
print(xyzzyy)

else:

print(‘Failed to get reading. Try again!’)

sys.exit(1)

I mean, i could install the module in python 2, but I’d rather not go that route, and get OH working properly with python3.

as OH runs with a different user ( openhab ) than the one you used at command line I would check where the module is installed. Is it installed in a globally available path or is it installed in your command line user’s environment/directory ?

1 Like

yeah that was the issue. made sure the DHT library was installed for all users.

Also was getting this error after fixing previous issue:

2022-10-24 10:46:31.235 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'change_hum-1' failed: For input string: "Traceback(mostrecentcalllast):
File"/etc/openhab/scripts/hum_bed.py",line12,in<module>
humidity,temperature=Adafruit_DHT.read_retry(sensor,pin)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py",line94,inread_retry
humidity,temperature=read(sensor,pin,platform)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py",line81,inread
returnplatform.read(sensor,pin)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py",line34,inread
raiseRuntimeError('ErroraccessingGPIO.')
RuntimeError:ErroraccessingGPIO.
Traceback(mostrecentcalllast):
File"/etc/openhab/scripts/hum_bed.py",line12,in<module>
humidity,temperature=Adafruit_DHT.read_retry(sensor,pin)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py",line94,inread_retry
humidity,temperature=read(sensor,pin,platform)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py",line81,inread
returnplatform.read(sensor,pin)
File"/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py",line34,inread
raiseRuntimeError('ErroraccessingGPIO.')

openhab did not have access to GPIO

fixed with:

sudo adduser openhab GPIO

(just in case anyone else runs into this issue)

thanks++

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.