Editing and Running Python Programs on the Rasberry Pi
The Raspberry Pi is a low-cost ($25US) 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 runningm 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.
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 atStart -> 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/csevThe 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.
csev@rasberrypi:~$ pwd /home/csev csev@rasberrypi:~$
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
csev@rasberrypi:~$ pwd /home/csev csev@rasberrypi:~$ cd Desktop csev@rasberrypi:~$ pwd /home/csev/Desktop csev@rasberrypi:~$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".
csev@rasberrypi:~$ pwd /home/csev/Desktop csev@rasberrypi:~$ cd .. csev@rasberrypi:~$ pwd /home/csev csev@rasberrypi:~$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.
csev@rasberrypi:~$ pwd /home/csev/Desktop csev@rasberrypi:~$ ls -l total 4 -rw-r--r-- 1 csev csev 15 Jan 9 15:17 firstprog.py csev@rasberrypi:~$
Running Your Python Program in the Terminal
Start the Terminal program, navigate to the proper directory and type the following command:
csev@rasberrypi:~$ python firstprog.pyThis 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.