python import of local module failing when run as systemd/systemctl service

I had a very similar issue converting an upstart heartbeat.conf to a systemd heartbeat.service, except with the requests module. The solution was to specify in the new .service what user to run it as:

[Unit]
Description=web server monitor

[Service]
WorkingDirectory=/home/<user>/
User=<user>
ExecStart=/home/<user>/heartbeat.py
Restart=always

[Install]
WantedBy=multi-user.target

Without the User=<user>, I was getting in the journalctl:

systemd[1]: Started web server monitor.
heartbeat.py[26298]: Traceback (most recent call last):
heartbeat.py[26298]:   File "/home/<user>/heartbeat.py", line 2, in <
heartbeat.py[26298]:     import requests
heartbeat.py[26298]: ImportError: No module named requests
systemd[1]: heartbeat.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: heartbeat.service: Unit entered failed state.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)