Python Autoexec for Your Raspberry Pi

by russ_hensel in Circuits > Raspberry Pi

4085 Views, 27 Favorites, 0 Comments

Python Autoexec for Your Raspberry Pi

Pratik_jain_dahod_python.JPG

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.

​Tools and Materials

DSCN0928.JPG

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

​Crafting the Snake

python.png

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 )

os.system( r'sudo mount -t cifs -o username="russ",password="all9s",workgroup="MSHOME",file_mode=0777,dir_mode=0777,nobrl //192.168.0.120/share/_Source/rPi  /mnt/share1' )

# -------------- leafpad ------------
# 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

#proc = Popen( [r"leafpad"] )

# -------------- leafpad ------------
# 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 >>

sudo leafpad ~/.config/lxsession/LXDE/autostart

and add the line at the end of the file :

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

and save

​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 >>

sudo leafpad /etc/inittab

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 >>

sudo leafpad /etc/profile

Scroll to the bottom and add the following line :

startx

and save