I have a systemd service that regularly reads the first line of a root-owned file, transforms it and then uses png_util:
import png_util
with open('root-owned-file', 'r') as f:
f.read()
...rest of logic...
Now, when the systemd daemon starts, it doesn't have access to the png_util library I installed with pip (pip install png_util) because that only installs it for the installing user. This also happens, when I start the script with sudo:
ModuleNotFoundError: No module named 'png_util'
If I read a file owned by me and execute the script normally as my user, everything works fine.
The systemd service:
[Unit]
Description=PNG
[Service]
ExecStart=/tmp/pngreader
[Install]
WantedBy=multi-user.target
Is the trick simply using pip install --user root and then setting the PYTHONPATH for the root user somehow?