How to write and run a python program on the Raspberry Pi

How to Write and Run a Python Program on the Raspberry Pi

In this post, I’ll give you a quick overview of what a Python program is, what Python programs can be used for, and how to write and run a simple Python program on the Raspberry Pi.

What is a Python Program?

Python is a very useful programming language that has an easy to read syntax, and allows programmers to use fewer lines of code than would be possible in languages such as assembly, C, or Java.

The Python programming language actually started as a scripting language for Linux. Python programs are similar to shell scripts in that the files contain a series of commands that the computer executes from top to bottom.

Compare a “hello world” program written in C to the same program written in Python:

Hello World Program in Python vs C Programming

Unlike C programs, Python programs don’t need to be compiled before running them. However, you will need to install the Python interpreter on your computer to run them. The Python interpreter is a program that reads Python files and executes the code.

It is possible to run Python programs without the Python interpreter installed though. Programs like Py2exe or Pyinstaller will package your Python code into stand-alone executable programs.

BONUS: Download the Raspberry Pi programming cheat sheet – a one page PDF guide with instructions on how to create and execute C programs, Python programs, and Shell scripts.

What Can a Python Program Do?

Like shell scripts, Python can automate tasks like batch renaming and moving large amounts of files. It can be used just like a command line with IDLE, Python’s REPL (read, eval, print, loop) function. However, there are more useful things you can do with Python. For example, you can use Python to program things like:

  • Web applications
  • Desktop applications and utilities
  • Special GUIs
  • Small databases
  • 2D games

Python also has a large collection of libraries, which speeds up the development process. There are libraries for everything you can think of – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific computing.

Many (but not all) of the things you can do in C can be done in Python. Python is generally slower at computations than C, but its ease of use makes Python an ideal language for prototyping programs and designing applications that aren’t computationally intensive.

How to Write and Run a Program in Python

We’ll only cover the basics of writing and executing a Python program here, but a great tutorial covering everything a programmer needs to know about Python is the book Learning Python 5th Ed. (O’Reilly) by Mark Lutz.

Installing and Updating Python

Python 2 and Python 3 come pre-installed on Raspbian operating systems, but to install Python on another Linux OS or to update it, simply run one of these commands at the command prompt:

sudo apt-get install python3

Installs or updates Python 3.

sudo apt-get install python

Installs or updates Python 2.

Opening the Python REPL

To access the Python REPL (where you can enter Python commands just like the command line) enter python or python3 depending on which version you want to use:

Python and python3 REPL on the Raspberry Pi

Enter Ctrl-D to exit the REPL.

Writing a Python Program

To demonstrate creating and executing a Python program, we’ll make a simple “hello world” program. To begin, open the Nano text editor and create a new file named hello-world.py by entering this at the command prompt:

sudo nano hello-world.py

Enter this code into Nano, then press Ctrl-X and Y to exit and save the file:

#!/usr/bin/python

print "Hello, World!";

All Python program files will need to be saved with a “.py” extension. You can write the program in any text editor such as Notepad or Notepad++, just be sure to save the file with a “.py” extension.

Running a Python Program

To run the program without making it executable, navigate to the location where you saved your file, and enter this at the command prompt:

python hello-world.py

Make a Python File Executable

Making a Python program executable allows you to run the program without entering python before the file name. You can make a file executable by entering this at the command prompt:

chmod +x file-name.py

Now to run the program, all you need to enter is:

./file-name.py

Here are some additional resources that will help you make the most out of programming in Python:

Hopefully you found this post useful. If you have any questions, feel free to leave a comment below. If you know anyone else that would enjoy this article, please share it! You can also get updates when ever we post a new article by subscribing!

  • wtf says:

    sudo nano is a very bad idea.

  • Bruno SXS says:

    Nice article, but you could have gone a step above and explained some of the cool libs focused on the pi.

    • Good idea! I was planning to do separate article just focusing on libraries. I could go really in depth into that topic so I just wanted to focus on the basics on this one.

  • tarun says:

    Wonderful artical…i started liking this python than c &c++

    Short and sweet
    Is there any basic book to learn this language..
    Plz ….let me know..

  • Jessica Ward says:

    Awesome post about “Python Program on the Raspberry Pi”. But i don’t have much knowledge of python programming. But i like your concept. Surely i will try this on my raspberry pi project. Thanks for sharing.

    Regard
    Jessica Ward
    http://www.raspberrypistarterkits.com

  • tri says:

    Awesome…

  • Edward Kierklo says:

    Nice elegant solution that worked flawlessly the first time.

  • Darren says:

    Hi,
    I want to run the hello.py this program, I can run the chmod +x /home/pi/test/hello.py. After that, I type ./home/pi/test/hellp.py. It shows a error message.
    -bash: ./home/pi/test/hello.py: No such file or directory.
    I’m sure the hello.py is in /home/pi/test/ .
    Do you have any idea to fix it?
    Thanks

    • Kyran says:

      Darren, you cannot put the . in front of a full path to “run” a script. you need to invoke Python to run the program. The way you are trying to run a program is a bash script, not python program. do this:

      python /home/pi/test/hello.py
      or cd into the directory, like this: cd /home/pi/test
      and then call the python program like this: python hello.py

      Good luck!

  • Waleed Alrashed says:

    Hamza Al-Omari since you’re gearing up for python, think that you might be interested in IOT devices, don’t you think??

  • But where is the concept of raspberry Pi

  • fred says:

    macey is a pal

  • They usually like to be “in” and updated when it comes to clothing and apparel.

    Note: people love to talk about themselves, so ask her a question, let her talk and you listen. There will be plenty of good food
    and drink offered up along with numerous vendors ranging in flowers, crafts, antiques and more.

  • gary says:

    On my Pi3 using Python 3 I need to enter () parins to enclose “Hello”. Also no semicolon is needed (but works OK). BTW, SUDO NANO does not work. I tried python3 in terminal and used Python 3.4.2 Shell (IDLE) both.

    This means there are syntax differences between differing versions and models. That takes away a LOT of the simplicity.

  • Lynn says:

    Awesome post, thanks for sharing maybe you should also visit http://programmer.science/category/python-programming-tutorial this site is great for python programming tutorial said my dad.

  • RON says:

    I am trying to write SPI protocol code using Python. Do you have any related articles or links?
    Thanks for the help.

  • Nicely described , well done

  • IsaacM says:

    Excellent “Python Program on the Raspberry Pi” post. With my introductory knowledge on python programming, I must say that it was greatly described. Luckily, it’ll come in handy in my Raspberry Project.

    Thank you
    Jessica Ward
    https://safetomatic.com

  • idris says:

    Good day sir,i really need your help regarding my project. Am working on home lock system with facial recognition.am using raspberry pi3 as my main component but my codes are in python 2 format.when about running the program,it’s given me a syntax error….pls kindly help

  • Tushar says:

    NIce Article. Keep it up.. Thanks for sharing this

  • Gary says:

    What Raspbian command line (Python) would I write in a Raspberry Pi to go directly to Netflix on line?

  • Really Great post, Thanks for sharing

  • Leave a Comment

    Your email address will not be published. Required fields are marked *

    Raspberry Pi Programming Cheat Sheet - How to Create and Run Shell Scripts, Python Programs, and C Programs

    Raspberry Pi Programming Cheat Sheet - How to Create and Run Shell Scripts, Python Programs, and C Programs

    Enter your name and email and it will be sent to your inbox:

    Consent to store personal information:

    Email me new tutorials and (very) occasional promotional stuff:

    Check your email to get the PDF!