Editing and Running Python Programs on the Rasberry Pi

The Raspberry Pi is a low-cost ($5 to $35US) complete Linux computer. It is a great platform for writing Python programs and learning about computers in general.

We won't cover getting a RasPi up and running here - but the nice thing is that once it is up and running there is nothing else needed to develop the programs for this class.

Writing "Hello World" on the Raspberry Pi

You can either view screen cast for these instructions on YouTube or you can download the high-quality MP4 version of the screen cast. You will need Apple QuickTime or some other MP4 viewer installed to view this video. The quality is much higher and the details are easier to read on the MP4 version.

Go to the lower-right of your screen and click on the Start icon and then go to Accessories -> Leafpad. Leafpad is the text editor.

Then create your first Python program.

    print "Hello World"

Save your program as firstprog.py onto your Desktop.

Starting Terminal

While there are many ways to run a Python program under Linux, we will prefer using the terminal program. There are several terminal programs on the Raspian Linux - my favourite is at

Start -> Accessories -> LXTerminal

LXTerminal has a nice preferences feature to allow you to change the size and color of the text.

Where Are You?

When LXTerminal starts up, you are in your "home" directory. In each of these examples, your logged in account should be used instead of csev.

    Home Directory: 		/home/csev
The command line prompt usually includes some clue as to where you are at in the folder structure on your hard drive. If you want to really figure out where you are, on Linux use the pwd command.
    [email protected]:~$ pwd
    [email protected]:~$ 

Where can you go?

Generally the first thing you want to do when you open a terminal program is to navigate to the correct folder. Say you wanted to run a file from your Desktop. The command is cd Desktop

    cs[email protected]:~$ pwd
    [email protected]:~$ cd Desktop
    [email protected]:~$ pwd
    [email protected]:~$ 
Nifty Trick: On the cd command, you can partially type a folder name like Desktop and then press the TAB key and the system will auto-complete the folder name if you have typed enough that the system can accurately guess what you mean to type.

Going Backwards (or Upwards)

You can change directory to the parent folder (the folder "above" the folder you are in using the cd .. command. It just says "go up one".

    [email protected]:~$ pwd
    [email protected]:~$ cd ..
    [email protected]:~$ pwd
    [email protected]:~$ 
If you get Lost...

If you can't figure out what folder you are in and/or cannot figure out how to get to the folder you want to get to "home" simply close and re-open the Command Line / Terminal window.

What Files/Folders are Here?

You can list the contents of the current directory using the ls -l command.

    [email protected]:~$ pwd
    [email protected]:~$ ls -l
    total 4
    -rw-r--r--  1 csev  csev   15 Jan  9 15:17 firstprog.py
    [email protected]:~$ 

Running Your Python Program in the Terminal

Start the Terminal program, navigate to the proper directory and type the following command:

    [email protected]:~$ python firstprog.py
This loads the Python interpreter and runs firstprog.py, showing the program output and/or errors in the Terminal window.

Some Cool Hints on The Terminal Program

You can scroll back through previous commands by pressing the up and down arrows and re-execute commands using the enter key. This can save a lot of typing.