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.
2. 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.
www.sf-innovations.co.uk
Method 1
This 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 up
Step 2: Check for Auto start file and if necessary create one
Step 3: Create desktop file for Python program
Step 4: Place this in the Autostart directory
Step 5: Test it out
Method 2
For advanced users – see last slide
3. Step 1: Make sure that the Pi starts up desktop on power up
From the command line prompt or LXTerminal, type “sudo raspi-config”.
You will get the screen below. For boot_behaviour select desktop. When
you 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. Step 2: Check for Auto start file and if necessary create one
On the taskbar of the Raspberry Pi open file manager.
Type /home/pi/.config in the address bar at the top. (Directories starting with
a dot are not normally shown, so you have to type this in).
www.sf-innovations.co.uk
If a directory “autostart” is not showing then under the file tab, use the option
to create a new folder called “autostart”.
5. Step 3: Create desktop file for Python program
Using the text editor “Leafpad” type the following. You can find Leafpad
under other applications in file manager.
Call this “mypythonprogram.desktop” and save in /home/pi/Desktop
www.sf-innovations.co.uk
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=mypythonprogram
Comment=
Exec=sudo python /home/pi/mypythonprogram.py
StartupNotify=false
Terminal=false
Hidden=false
6. Step 4: Place this in the Autostart directory
Use 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.uk
Unplug the power supply and reconnect it. Your Python program should start
running automatically after a few seconds.
7. Summary
Hope this presentation has been useful in getting started with Python on
the Raspberry Pi. The book “Programming the Raspberry Pi” by Simon
Monk is a useful introduction to Python.
www.sf-innovations.co.uk
Keep an eye on our website www.sf-innovations.co.uk for any updates to
this presentation, new Custard Pi layers or new presentations.
8. Method for advanced users
First code and debug your Python program. Make sure that the Pi starts
up desktop on power up, as described in Step 1 earlier.
Then we need to edit /etc/rc.local by typing
sudo nano /etc/rc.local
www.sf-innovations.co.uk
Note: Nano is a command line editor. You navigate round the text by
using the cursor keys or CTRL key commands which are
summarised 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.py
Now every time you power up, “yourprogram.py” will run.
(Thanks to Cambridge University Computer Labs for this.)