
Running a Python Program on boot with the Raspberry Pi
One of the most common questions asked about the Raspberry Pi is how to get code you wrote to run automatically as soon as you boot it up. We get why this question is so common; a lot of people have entered this world of open electronics by way of the Arduino, and simply giving it power will start it up and run your code. With the Pi, there is a lot more going on so in this tutorial we will show you how to get your code up and running automatically as soon as the Pi starts.
There are a lot of different operating systems out there for the Raspberry Pi, so we are going to focus on the most popular: Raspbian. We are using the version dated: 2018-06-27 deviating from this may result in different / additional steps so if you are very new to this, we recommend following the tutorial exactly. Please Note: This will not work on the Lite version of Raspbian.
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!
Be aware that there seems to be a limit to the number of characters allowed in the path/filename string in the line added to rc.local. It is somewhere between 29 and 45 in that 29 works for me and 45 doesn’t. This had me stumped for months. I use Python 3. Thinking back to DOS and shortening the file name did the trick.
In your example line,
“python (/home/pi/weather.py) &”
the string between the brackets is where the character count is critical.
Always test your line for correct function in a Terminal window. If it works there it will work in rc.local. If it doesn’t work in a Terminal window, then at least you will get an error message. In my case ‘unable to find file’ error meant the string is too long.
Nik – interesting point! We haven’t ever come across that issue but testing in a terminal window is always very good advice!
thanks, but what if i want to run a script on start up and the scripts prints some outputs which i should see them on the terminal, what can i do
this method wont worked if u have input() on your code
katunziv,
Have you tried arranging for your Pi to boot into a CLI (a terminal window) rather than the Desktop. You should see your printed outputs then.
To do this go :-
‘Applications Menu’>’Preferences’>’Raspberry Pi Configuration’ and in the ‘Systems’ window change from ‘Boot to Desktop’ to ‘Boot to CLI’ and reboot.
When you’ve finished and wish to return to the Desktop just type ‘startx’. You can then re-edit the configuration and reboot again.
This is helpful. How do I “compile” my python program, or otherwise hide the source code from the user?
Thank you
Hi. I am pretty new to this. I am using a Pi zero, and the newest release of the Raspberry Pi operating system. My code runs in a terminal window without a problem. I followed your directions exactly. It seems the code loads to a certain extent. LED flashes as it should, but I get no response from my WiFi keypad. I have tried several other methods as well to run code on boot, mostly with the same result. Any info would be greatly appreciated. Thanx.