# Python Autoexec for Your Raspberry Pi

2,473

27

## Introduction: Python Autoexec for Your Raspberry Pi

I have been playing a lot with my Raspberry Pi and after I start it up I often issue the same set of commands. It is a pain in the neck and error prone to enter them each time by hand. In the old days of DOS we dealt with this by using a batch program, just name it autoexec.bat and put it in the root of your boot disk and you are done. On the Pi there are many ways dealing with this but I decided it would be nice to use Python as much as possible. This comes out a lot like the old autoexec.bat on DOS. Since Python is pretty much the official language of the Pi ( and a great language in general ) this seems like “good idea” In this set up nothing happens until you start the desktop ( for example with startx ) and works for both local and remote ( xrdp type in my testing ) desktops.

## Step 1: ​Tools and Materials

• Rasbery Pi, Installation should include Python and Idle, standard

## Step 2: ​Crafting the Snake

I will assume that you have a version of Python installed ( I like For Python I like the Spyder environment as described in Graph Instructable Views with Python Screen Scraping by russ_hensel ) and have at least some idea of how to use it. I also assume you log in as pi. Lets create a python program called \home\pi\autoexec.py. What you put in it depends on what you want to do. I will illustrate a few possibilities just by giving you a commented file here (in a box). To get your copy you can just cut and paste, no download needed. Note that this Python program can be extended to do anything any Python program can do, the contents here are just the statements that I found particularly useful.

# =============== begin autoexec.py ============
# this is an example file, it shows how to open stuff, not
# a good example of what to open, which is up to you.
# this opens up so much stuff you may want to try it
# with some lines commented out.

# you can run it onthe Pi as a normal idle or spyder program you do
# not have to reboot each time

import os
from subprocess import Popen,PIPE

# normally the output really has no place to go so nothing will be seen from the print statment
print "starting autopython.py"

# -------------- mount an nas drive  ------------
# use os.system only if the command runs and exits othwise this program
# will stop until command is done ( not my real password )

# this will run leafpad ( a nice little editor on the Pi ) with no file opended, the program does
# not wait to see how leafpad runs but goes on to the rest of the program
# not waiting is a general feature of Popen as used here

# open a file, actually tis file in leafpad

proc = Popen( [ "leafpad /home/pi/autopython.py"], shell=True ) #

# -------------- starpusher ------------
# run the python game starpusher

#proc = Popen( [r"python /home/pi/python_games/starpusher.py"], shell=True )

# -------------- idle ------------
# just open the idle ( a python environment

#proc = Popen( [r"/usr/bin/idle"] )

# -------------- idle editing a file ------------

#proc = Popen( [r"/usr/bin/idle  /home/pi/python_games/starpusher.py"], shell=True )

# -------------- idle not editing a file but running it  ------------
# not a particularlly useful process

proc = Popen( [r"/usr/bin/idle -r /home/pi/python_games/starpusher.py"], shell=True )   # works, but runs program

print " autopython.py all done "

# =================== eof  ========


There is one additional step to making this work, you need to edit one of your Pi's configuration file:

use the editor leafpad, in a terminal enter >>

and add the line at the end of the file :

@/usr/bin/python /home/pi/autoexec.py

and save

## Step 3: ​Adding Auto Logon and Startx

You do not need this if you are running with an xrdp remote desktop ( and it seems to kill it ) but if you are not running it you make like a bit more automation.

• Automatic logon:

In a terminal enter >>

comment line similar to the one below by putting a '#' in front of it, such as:

#1:2345:respawn:/sbin/getty 115200 tty1

now add the line, beneath it:

1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

and save

• Automatic startx: In a terminal enter >>

Scroll to the bottom and add the following line :

startx

and save

34 6.6K
66 8.2K
271 26K
217 22K