I'm trying to daemonise a python script using systemd, but constantly get the error "No module named 'oandapyV20'" after activating the daemon.
The script is at location: /home/user/workingdir/script.py
The virtual environment is at: /home/user/venv/bin/
My best guess at how to build the service from docs I've found is as follows:
[Unit]
Description=DataLoader
[Service]
User=root
Group=root
WorkingDirectory=/home/user/workingdir
ExecStart=/home/user/venv/bin/python3 script.py
[Install]
WantedBy=multi-user.target
What does work...
python3 script.py
or activating virutal environment
source /home/user/venv/bin/activate; python3 script.py
Although that works outside of the service, nothing I've tried works when calling from systemd.
Where am I going wrong? What am I not realising?
Eventual solution (with little understanding)
[Unit]
Description=DataLoader
[Service]
User={user_name}
Group={user_name}
WorkingDirectory=/home/{user_name}/workingdir
ExecStart=/usr/bin/python3 script.py
Restart=always
[Install]
WantedBy=multi-user.target
sudo su -(with hyphen) and executecd /home/user/workingdir; /home/user/venv/bin/python3 script.py? Is it different withsudo su(no hyphen)? If so, I'd scrutinize differences in environment variables. You might also runsource /home/user/venv/bin/activate; which python3to make sure you're actually calling/home/user/venv/bin/python3as you currently believe.envcommand to print all environment variables, and usesource /home/user/venv/bin/activate; which python3to ensure thepython3you're calling is thepython3you think you're calling. If it's calling a differentpython3, I'd especially inspect differences in $PATH.source /home/user/venv/bin/activate, it's not setting your$PATHor ($PYTHONPATH) (at least not to what you had thought.) Hopefully that goes a long way toward explaining the discrepancy.