How to Autorun/Autostart Your Python Script in the Raspberry Pi OS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to bold guy diy in this video i'm going to show you how you can auto run a python script as soon as your raspberry pi boots to the desktop and trust me if you've spent hours looking all over the internet to find the right method i promise this one actually works [Music] [Applause] i looked all over the place to try to find a way to auto run or auto start a python script when i boot up the raspberry pi now there are lots of options online for doing that so that when the command line interface loads it auto runs a script but there are none that seem to be available for the desktop or graphical user interface that was extremely frustrating i tried all of the methods they mentioned and every single time i'd get to the desktop nothing would happen and i'd have to try something else i finally found the answer in a forum about the raspberry pi where a user submitted the way that they do it and so i'm going to show you that here and i'm also going to tell you how you can strip it down so that you get the bare essentials of what's necessary to load your python script on boot so without further waste of time let's get started so just before we get into the method necessary to do this in the graphical user interface let's take a quick look at the methods that will work if you're using a command line script that works fine from the command line any of these methods as you look them up online they'll be commonly found and by editing those files and adding different commands you'll be able to find something that works to launch your script from the command line however if you need to launch from the graphical user interface or desktop instead this is the method that you do we're going to create a new file with a dot desktop extension and we're going to put it in the home pi config auto start folder all of those entries that you see below starting with desktop entry and ending with the terminal are all switches and commands that are necessary for that particular file in order to make it work properly in the end i'll show you how you can strip out most of those things and get down to the bare essentials the script that i'm going to be using is just a very simple user interface that's created using the pi simple gui module and the great thing about this is very easy it uses just the one module and it uses this layout here which basically defines the three different rows that i'm going to use first row is going to have some text that says gui auto run at startup second row is going to say it works click exit to go to the desktop and the third row is simply going to be an exit button of course this is super simple just to demonstrate how well it works the settings in the window command are going to be what causes it to actually work full screen there are three different items here that you need to get right in order to take up the entire width and height of the raspberry pi screen i'm using the touch screen here it uses an 800 by 480 pixel display so i need to put that into the size and i need to make sure that i add this finalize equals true switch because the window.maximize here below does depend upon it so when i use the size the finalize and the window maximize together i am now able to fit the entire full screen of the raspberry pi touchscreen this event loop where that begins with while true is where all of the events from the different widgets or elements are read and all of their values are stored using that window.read function there is a print command here that's going to print the event and values to the terminal window which you don't really need except for the point of troubleshooting where it comes very handy and then every one of these persistent windows also comes with an if statement which is going to break out of the true loop if if the window is exited which causes the window to close now i'm just running this on my desktop you're going to see here that that's what it's going to look like and when i put it onto the raspberry pi it's going to fill the entire screen now here i've copied the file that i want to my raspberry pi desktop so in order to actually set it up i'm going to open the folders and i'm going to look here for the dot config folder now if you haven't noticed it isn't there by default you have to show the hidden folders and then you'll see the dot config folder and there should be a folder called auto start now the first time i started playing with this there actually wasn't a folder called auto start so if you take a look and there isn't one you simply need to create it you're just going to right click here and choose new folder and you're going to call it auto start all lower case letters and that's going to let the gui interface know that it needs to read something from that folder now you're going to create a new empty file inside of that folder and you can call it whatever you want the most important thing is that the extension is dot desktop now we're going to open that file that we created and inside we're going to paste that information that i listed there which are all the different switches and commands needed in order to create a proper file the most important one here that you'll need to change is the one that actually displays the path to your particular file mine is left on the home pi desktop folder and so if you just follow that through you can put yours wherever you'd like but make sure you put the correct path in now that we have that there we're going to save it we're going to reboot and let's see if it actually loads our particular file when the graphic user interface starts you find here that it's always a little bit nerve-wracking as you fire the whole thing back up and let it start and do its thing and you're never sure if it's actually going to work after doing this so many times and trying different methods i was worried that it was never going to work but sure enough once it's set up properly in this folder as soon as the desktop opens you see it for just a second and then boom your script runs and you can see it there so i can click the exit button as i mentioned that i defined earlier and it did exactly what it was supposed to do now if we go into this file that we created you can see here it's called gui controller because that's what i put as the name we can strip out all of this information except for two lines and the two lines that are important are the ones in square brackets at the very beginning the first line there that says desktop entry and the other one that's really important is the exact line or executable line which also has the path to our file those two are the ones that are absolutely necessary the rest of the things might be necessary depending on how your particular script is going to run perhaps it needs some of those other switches to be enabled for my case i was able to strip all the rest of them away really just be able to put the desktop entry field and also the path and have it work perfectly as it started back up again as you can see here as it all loads up again that my script once the desktop loads immediately loads right after once again it's ready to go and i can use either my mouse or reach over with my finger and of course tap it and use the touch screen features as well so now you can see hopefully how easy it is to set up one of these files so that it'll boot properly when you get to the desktop of your raspberry pi i hope that solves the problem and answers the question of how you can run a script when your raspberry pi first boots up into the graphical user interface and you can breathe a sigh of relief knowing that it'll work on your future project i know for me i'm super satisfied that it works there's many more gui projects that i want to do in the next few videos if you like the videos we're making in this type of content please give the video a thumbs up subscribe to the channel so you'll be notified of future videos that come our way and keep checking back every week as i post a new video every saturday morning until next time in all your pursuits of just the right code that finally works don't be afraid to be balder
Info
Channel: BaldGuyDIY
Views: 22,374
Rating: undefined out of 5
Keywords: how to autorun python script at boot, how to autostart python script at boot, how to autorun python script raspberry pi, how to autostart python script raspberry pi, how to autorun a python script on raspberry pi boot, raspberry pi 3 setup, raspberry pi 4 setup, raspberry pi computer diy, raspberry pi projects, rc.local startup script
Id: cZa1oCSdbUk
Channel Id: undefined
Length: 8min 2sec (482 seconds)
Published: Sat Aug 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.