For context, I am using Raspberry Pi model 3B+. Currently I am trying to run a python script at the Pi's boot up. The script uses the module face_recognition, and everything works fine when running it normally or through the terminal. But as soon as I try running it automatically when the pi boots up I get the following error:
Traceback (most recent call last):
File "/home/pi/Desktop/code/please_work_2.py", line 6, in <module>
import face_recognition
ImportError: No module named face_recognition
I googled a bit and I think it has to do something with not setting the environment in the service file correctly. It is a bit messy as of right now, but I am new to working with these kind of files so I am struggling with finding out how to get it to work. My service file right now:
[Unit]
Description=Start Bling
[Service]
Environment=DISPLAY=:0
WorkingDirectory=/home/pi/facial_recognition
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity
[Install]
WantedBy=graphical.target
The program does not necessarily need to run in Desktop auto-login so if there is a possible fix in the console version that is fine as well. I just have it this way currently, so it is easier to check if the program is working as intended.
EDIT: I have also tried using crontab, but then nothing happened on reboot. Not with and not without the '&' at the end. As of right now, as was suggested below is that when I add
User = pi
the program does launch, but I am struggling with getting it to launch once instead every 10s. Deleting that bit does not help since then it stops launching at all.
Can you try this, add the following User=pi
under [Service]
and see if it works.
I think the way you did it, you are trying to launch as another user or sudo user, you have your stuff only installed in your current pi user path, so try launching the script as the user pi.
Your service file should look like this.
[Unit]
Description=Start Bling
[Service]
User=pi
Environment=DISPLAY=:0
WorkingDirectory=/home/pi/facial_recognition
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity
[Install]
WantedBy=graphical.target