How To Run Python On A Raspberry Pi

by | Apr 1, 2017

Executing Python

 

Python Foundation 2 snakes logoSo you’ve decided to take the plunge and learn more about computers and programming with a Raspberry Pi, you’re ready to start writing your own amazing projects or trying other peoples code but then you realise that your not to sure how to actually run a program. Well, like most things to do with the Raspberry Pi it is quite easy to do and in this tutorial I will show you how to do it in a couple of ways. Firstly using the Pi desktop connected to a keyboard and monitor and then using programs like putty and filezilla to run the code remotely.

The Raspberry Pi can execute code written in a wide range of languages, given that the “Pi” in Raspberry Pi is a nod to the Python language itself and that it is a relatively easy to learn for the beginner, it’s a great place to start.  In the descriptions below I am assuming that you have setup your RPi and that SSH is enabled, also that you are using the latest version of Raspian and the Pixel desktop.

 

Running the Code from the Pixel Desktop

 

In order to execute a program the best place to start is with a piece of software called an IDE (Integrated Development Environment), IDE’s allow you to create, edit and execute your code, in the case of the Raspian operating system on the RPi it comes with two, one for working with Python 2 and another for Python 3. IDE’s are also helpful to programmers as they can assist you in debugging and finding errors in your code.

The Python language has changed over the years and as such the commands and syntax have been updated and developed, the examples used in this tutorial will be for Python 3.

The IDE software that comes with Raspian is called IDLE (after Eric Idle of Monty Python fame), to open it you need to go to the main Raspberry Menu, select Programming and then Python 3 (IDLE) as shown below.

 

Raspberry Pi open python 3 IDLE from the desktop

 

With IDLE now open we can begin to create some code, we’ll start with the old classic “Hello World”, enter the following code into the IDLE editor and press enter, as this IDE is designed for Python you will notice that the colour of what you are writing changes as it recognises the syntax.

 

print ("Hello World")

 

Execute Hello World Python code in shell

 

Congratulations you have now run your first Python program. As this is a Python IDE you can also create programs with loop statements and the IDE will automatically provide the correct indentation to run the code. As an example type in the following code and press enter.

 

for i in range (0, 6):
    print ("The current number is ", i)

 

Notice that the syntax has been colour coded, the print statement has been indented and the “for” loop has been executed the required number of times.

 

Execute Python code from directly in shell

 

Ok, so this is all fine as an example of how IDLE works but in the real world running programs this way isn’t very helpful, what you want is a way to write longer programs and be able to run them over and over again. To do this all we need is a simple text editor, from the IDLE window select “File” from the menu and then “New File” (or Ctrl+N), this will open up a new window and we can begin writing our code.

 

#!/usr/bin/env python3

print ("Hello World")

for i in range (0, 6):
    print ("The current number is ", i)

 

 

Now with our code written we need to save it, select “Save As…” from the file menu and give you code a name, note that all python scripts need to end with the suffix .py. You may also notice the first line in the code above, this is known as a “shebang” and serves to let the system know what Python interpreter to use when it runs the program. To execute the program simply select the “Run” option from the menu and then “Run Module”.  Your results of your program will then be shown in the IDLE window.

 

Run Python module in Raspberry Pi IDLE

 

Execute saved Python code in IDLE

 

Another way to execute your code once it has been saved is directly from the Command Line Interface (CLI) or terminal screen. To do this you first need to open the terminal on your desktop, select the Applications menu (Raspberry icon), then the Accessories menu and finally select Terminal

 

Open Terminal screen on a Raspberry Pi desktop

 

This will present you with a screen that contains the command prompt, to run our saved code you first need to navigate the file system to the folder that you saved your script in, this can be done using code similar to this but obviously using your own folder names.

 

pi@raspberrypi:~ $ cd /home/pi/myprojects

 

Once you have navigated to the correct folder you can enter one of the following commands depending on whether you want to run the code using Python 2 or 3 as shown in the image below.

 

pi@raspberrypi:~ $ python Example.py

Pi@raspberrypi:~ $ python3 Example.py

 

Execute Python code from terminal window

 

As I said earlier the code here is written for Python 3.x and as such you can see there are some slight differences in the way the print command is interpreted using the different versions.

 

Running the Code Remotely

 

If you plan on using your Pi in a Headless mode (ie. no monitor or keyboard attached) then you will need to have some additional software on your computer to enable you to transfer and execute your code. You also need to have enabled SSH on your Pi so that a terminal emulator and FTP client can talk to it, this is best done by having a monitor and keyboard attached initially and running “Raspberry Pi Configuration” from the applications menu.

 

Raspberry Pi Configuration Window

 

The 3 pieces of software that you will need are

  1. Text editor or IDE  (notepad or NINJA)
  2. Terminal Emulator  (PuTTY)
  3. FTP Client  (Filezilla)

All of these programs are free to download.

We’ll begin by recreating and saving the code from above on our local computer in a text editor like notepad, don’t forget to save your file with the suffix .py

 

Python code written in notepad

 

Next we need to transfer this file to the Pi, to do this we’ll use the FTP client Filezilla. If you know the IP address of your Pi then you enter this as the Host, if you don’t know your IP address then this can be found by logging into your router (type 192.168.1.1 or 192.168.0.1 into a browser bar) and selecting the DHCP entries where you should see the host name “raspberrypi” and the associated IP address.

Alternatively if you know the host name of your Pi (default is “raspberrypi”) then you can just type “raspberrypi.local” as the Host and it will find the IP address for you, next enter the username (default = “pi”), password (default = “raspberry”), set the port to 22 (sftp) and click “Quickconnect”.

 

Filezilla main screen

 

Filezilla will now display your local files on the left hand side and the files of your Pi on the right, navigate your local file system on the left to find the file that we just saved, navigate the files of your Pi to where you want to save your code too then simply drag and drop the file across. You can check whether the transfer was completed by checking the ” Successful transfers” tab at the bottom of the screen.

Now that you have successfully got you code onto the Pi we need to access the command line of the Pi, to do this you need to run the Putty terminal emulator,  again enter your IP address or hostname into the Hostname box, select port 22 and SSH from the radio buttons and click open.

 

Putty connection using IP address

Putty connection using hostname

 

You now have access to terminal on your Pi and you can execute the code in the same way that you did when we were doing it from the desktop as shown below.

 

Python code executed remotely on a Raspberry Pi

 

So there you have it, using these techniques you should have no problem executing Python code on your Raspberry Pi.

If you are new to the Pi and want to find out more about what you can do with it then why not check out my review of Adventures in Raspberry Pi by Carrie Anne Philbin of the Raspberry Pi foundation.

If learning Python and applying it to practical situations is more your thing then why not check out Automate the Boring Stuff by Al Sweigart to get the basics in a way that may be useful in the real world.

 

If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.

 

Pin It on Pinterest

Share This