-2

I have a code set up to run at boot, and it works fine when I start it via terminal, but it doesn't run at boot. I opened the terminal, and this is what I saw:

Running at boot
Traceback (most recent call last):
  File "/home/pi/program.py", line 2, in <module>
    import wiringpi
ImportError: No module named wiringpi

I know I have it on the pi as it works when I start it manually, but for some reason it doesn't work at boot. Does anybody know how to fix this?

10
  • 1
    What do you run at boot? How do you run it? What terminal do you open to see the message?
    – Ingo
    Commented Jul 26, 2020 at 19:23
  • @Ingo, i run a python program from bash.rc at boot and when i open the terminal. i open the terminal the pi comes with by default.
    – user121626
    Commented Jul 26, 2020 at 19:36
  • What bash.rc do you mean? In /etc/ is no bash.rc. There is a /home/pi/.bashrc (with leading dot). Do you mean that? What operating system do you use? What version of it?
    – Ingo
    Commented Jul 27, 2020 at 8:31
  • @Ingo yes. I use raspbian. i updated and upgraded everything just yesterday.
    – user121626
    Commented Jul 27, 2020 at 14:01
  • What bash.rc do you mean?
    – Ingo
    Commented Jul 27, 2020 at 17:00

3 Answers 3

4

You cannot use ~/.bashrc to run a program at boot up. It is to configure bash and runs only with bash sessions and not at boot up of the RasPi. To have a service started on boot up you best use a systemd Unit file. Try it by creating a Unit file with:

rpi ~$ sudo systemctl --force --full edit myprogram.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=My python3 script
After=multi-user.target

[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/program.py

[Install]
WantedBy=multi-user.target

Manage it with:

rpi ~$ sudo systemctl enable --now myprogram.service
rpi ~$ sudo systemctl status myprogram.service
rpi ~$ sudo systemctl stop myprogram.service
rpi ~$ sudo systemctl start myprogram.service
1

An alternative to systemd for starting a program at boot is cron. If you've not used cron to schedule jobs, you may find it easier than systemd. Here's how:

  1. Open the "scheduler" for cron - the crontab:
$ crontab -e

This will open your crontab in the editor you have chosen (nano is a good choice)

  1. Add the job to the crontab - add the following line in the editor:
@reboot ( /bin/sleep 15; /home/pi/program.py >> /home/pi/cronjoblog 2>&1 )
  1. Save the file, exit the editor, and reboot to test. If there are errors they will be written to the file /home/pi/cronjoblog
0

I think what's happening is the module is present, but not where it's being looked for. 2 things, running at boot it may be running as root. This changes paths among other things, also, there's when it's started relative to other things being set up. So, first, try using the full path name ( import /... ??/wiringpi ) ( wherever it is ). If that doesn't work, delay the start. The "correct" way to start jobs on boot ( I don't know it well ) does have a way to say either "requires" or "wait for".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.