Raspberry Pi - Autostarting a Python Program

38,642 views

Published on

When the Raspberry Pi is used in control applications, there is often no need for a keyboard, mouse or monitor. One is just using it to monitor inputs and control some outputs in a particular application.

In this case, the user just wishes to start the Python program when the Raspberry Pi board is powered up.

Published in: Technology
6 Comments
7 Likes
Statistics
Notes
  • So I got this to very nearly work, but now have to restart from scrath. Is there no way to stop the script? My python script (which plays an mp3 through headphones whenever a button is pressed) runs on startup, after initialising my audio ports, and GPIO. But mpg321 can't find the mp3! It works fine when running it from command line. It's in the same directory as my python script /home/pi/pi-phone.py so now it appears I have to start over again. My guess is that I didn't specify the folder to find the mp3 in and it needs this when starting from rc.local . Hey ho, time to reinstall wheezy and start again.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I have tried this and it does not work. I have tried putting the text of my Exec= line in the terminal and it will run. But the script will not run.

    Here's what I have

    Exec=sudo python /home/pi/valley.py

    If I have that line 'sudo python /home/pi/valley.py' in the terminal, it runs, so I'm not sure what I am doing wrong.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • @JibinJames1,
    Your path is probably wrong; in order for that to work, your file has to be in the /home/pi/ directory. If your program is on the desktop, change that line to 'Exec= sudo python /Desktop/try.py'
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • the system runs automatically as it doesnot ask the login and password but it doesnot run my script. The name of the python scriot is try.py and in the above i wrote try.py instead of pythonprogram.py and the rest i did the same as mentioned above.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • How do you cancel the script? Ctrl+Z doesn't work.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
38,642
On SlideShare
0
From Embeds
0
Number of Embeds
165
Actions
Shares
0
Downloads
0
Comments
6
Likes
7
Embeds 0
No embeds

No notes for slide

Raspberry Pi - Autostarting a Python Program

  1. 1. www.sf-innovations.co.ukRASPBERRY PIAuto starting a Python program on power up
  2. 2. When the Raspberry Pi is used in control applications, there is oftenno need for a keyboard, mouse or monitor. One is just using it tomonitor inputs and control some outputs in a particular application.In this case, the user just wishes to start the Python program when theRaspberry Pi board is powered up. www.sf-innovations.co.ukMethod 1This presentation is a step by step guide on how to set that up.Step 1: Make sure that the Pi starts up desktop on power upStep 2: Check for Auto start file and if necessary create oneStep 3: Create desktop file for Python programStep 4: Place this in the Autostart directoryStep 5: Test it outMethod 2For advanced users – see last slide
  3. 3. Step 1: Make sure that the Pi starts up desktop on power upFrom the command line prompt or LXTerminal, type “sudo raspi-config”.You will get the screen below. For boot_behaviour select desktop. Whenyou next power up, the Raspberry Pi will load up the GUI automatically(without you having to type in “startx”. www.sf-innovations.co.uk
  4. 4. Step 2: Check for Auto start file and if necessary create oneOn the taskbar of the Raspberry Pi open file manager.Type /home/pi/.config in the address bar at the top. (Directories starting witha dot are not normally shown, so you have to type this in). www.sf-innovations.co.ukIf a directory “autostart” is not showing then under the file tab, use the optionto create a new folder called “autostart”.
  5. 5. Step 3: Create desktop file for Python programUsing the text editor “Leafpad” type the following. You can find Leafpadunder other applications in file manager.Call this “mypythonprogram.desktop” and save in /home/pi/Desktop www.sf-innovations.co.uk[Desktop Entry]Encoding=UTF-8Type=ApplicationName=mypythonprogramComment=Exec=sudo python /home/pi/mypythonprogram.pyStartupNotify=falseTerminal=falseHidden=false
  6. 6. Step 4: Place this in the Autostart directoryUse the File Manager to cut and paste this file into the/home/pi/.config/autostart directory.Step 5: Test it out www.sf-innovations.co.ukUnplug the power supply and reconnect it. Your Python program should startrunning automatically after a few seconds.
  7. 7. SummaryHope this presentation has been useful in getting started with Python onthe Raspberry Pi. The book “Programming the Raspberry Pi” by SimonMonk is a useful introduction to Python. www.sf-innovations.co.ukKeep an eye on our website www.sf-innovations.co.uk for any updates tothis presentation, new Custard Pi layers or new presentations.
  8. 8. Method for advanced usersFirst code and debug your Python program. Make sure that the Pi startsup desktop on power up, as described in Step 1 earlier.Then we need to edit /etc/rc.local by typingsudo nano /etc/rc.local www.sf-innovations.co.ukNote: Nano is a command line editor. You navigate round the text byusing the cursor keys or CTRL key commands which aresummarised at the bottom of the screen.At the bottom, just above exit 0 we’ll add a call to our script.sudo python /home/pi/yourprogram.pyNow every time you power up, “yourprogram.py” will run.(Thanks to Cambridge University Computer Labs for this.)

×