CONTEXT
Ubuntu 22.04 Desktop: MQTT server
Goal: run python (paho with logger) script as a service
.py logging routine tests as expected (not in an venv):
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
OBSERVATIONS
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 <module>
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 paho module can not be found in line 6: import paho.mqtt.client as mqtt
QUESTIONS
Error returned: ModuleNotFoundError: No module named 'paho'
Why is the paho module not found?
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?
MQTT / Paho INSTALL
sudo apt update
sudo apt-get install mosquitto
sudo vi /etc/mosquitto/mosquitto.conf
allow_anonymous true
listener 1883
sudo systemctl restart mosquitto
sudo apt install python3-pip
pip3 install paho-mqtt
sudo apt install -y mosquitto-clients