22.04 – python script fails to start in service

A MQTT .py logging routine tests as expected (not in an venv, but from the command line):

python3 mqtt2log.py

line 6 of the script is: import paho.mqtt.client as mqtt

A .service file is coped to /etc/system/system:

# run python script as service
# https://unix.stackexchange.com/a/634422/182280e
# copy this .service description to /etc/systemd/system/myscript.service
# /home/user/mqtt2log.py

[Unit]
Description=mqtt2log.py

[Service]
ExecStart=/usr/bin/python3 /home/user/mqtt/mqtt2log.py

[Install]
WantedBy=multi-user.target

ls -l /etc/systemd/system/mqtt2log.service returns:

-rwxrwxrwx 1 root root 314 Jan 11 17:41 /etc/systemd/system/mqtt2log.service

sudo systemctl status mqtt2log returns:

Jan 31 13:01:30 mqtt systemd[1]: Started mqtt2log.py.
Jan 31 13:01:30 mqtt python3[158388]: Traceback (most recent call last):
Jan 31 13:01:30 mqtt python3[158388]:   File "/home/user/mqtt/mqtt2log.py", line 6, in 
Jan 31 13:01:30 mqtt python3[158388]:     import paho.mqtt.client as mqtt
Jan 31 13:01:30 mqtt python3[158388]: ModuleNotFoundError: No module named 'paho'
Jan 31 13:01:30 mqtt systemd[1]: mqtt2log.service: Main process exited, code=exited, status=1/FAILURE
Jan 31 13:01:30 mqtt systemd[1]: mqtt2log.service: Failed with result 'exit-code'.

The error messages seem to indicate the module paho module can not be found in line 6: import paho.mqtt.client as mqtt

What diagnostic steps can be taken? I am puzzled by the fact that the python script runs from the command line. Maybe this is somehow a permissions issue?

Read more here: Source link