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 realize that you’re not too 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 relatively easy to learn for the beginner, it’s a great place to start.  In the descriptions below I am assuming that you have set-up your Pi and that SSH is enabled, also that you are using the latest version of Raspbian.


Running Code with Thonny IDE


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, fortunately the Raspbian operating system on the Pi it comes with one called Thonny. 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.

To open Thonny you need to go to the main Raspberry Menu, select Programming and then Thonny Python IDE as shown below.




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


print ("Hello World")


Thonny Hello World


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


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


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


Thonny For Loop


Ok, so this is all fine as an example of how Thonny 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. Thonny has a built in text editor and it can be seen just above the Shell tab (currently <untitlied>). In the text tab enter the following 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 Current Script” or press the green “Play” button.  The results of your program will then be shown in the Shell tab.


Thonny run from menu


Thonny Run from Play Button


Thonny Run Output


Finally, adjacent to the green Play button in Thonny is the Debug button, pressing this button puts the IDE into debug mode, this can be very helpful when trying to identify errors in your programs. In Debug mode you are able to step through the program one line at a time and step into and out of any loops, you can also track any variables to make sure that all parts of your program are operating as expected.


Execute Code from the Command Line


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

Pi@raspberrypi:~ $ python3


Execute Python code from terminal window


As I said earlier the code here is written for Python 3 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. A text editor  (Notepad++ or Visual Studio Code)
  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 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 or into a browser bar) and selecting the DHCP entries where you should see the hostname “raspberrypi” and the associated IP address.

Alternatively if you know the hostname 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 your 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 the 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