Skip to content

How-to / Raspberry Pi

Set up Python on Raspberry Pi

Python is a powerful, flexible, and beginner-friendly programming language that comes pre-installed on Raspberry Pi OS. Learn how to verify your Python installation, choose an IDE, and run your first scripts.

BeginnerPython · ProgrammingBy Vilros

Check if Python is installed

Python comes pre-installed on Raspberry Pi OS, so you typically don't need to install it manually. To verify Python is available:

  1. Open Terminal on your Raspberry Pi.
  2. Type the following command and press Enter:
    python3 --version
  3. You should see output like Python 3.11.2 or similar, showing your installed Python version.

If Python is installed (which it should be on a standard Raspberry Pi OS installation), you're ready to start coding. If you receive an error saying python3 is not found, continue to the installation steps below.

Installing Python (if needed)

If Python is not installed, follow these steps carefully:

  1. Update your system packages:

    sudo apt update

  2. Install Python 3 and development tools:

    sudo apt install python3 python3-pip idle3 -y

  3. Verify the installation:

    python3 --version

Most users won't need manual installation, as Python 3 is part of the standard Raspberry Pi OS image.

Use Python on your Pi

You can use Python in two main ways: interactively in the Python shell, or by writing and running scripts.

Python interactive shell

The Python shell (also called REPL - Read-Eval-Print Loop) lets you try Python commands immediately and see results. It's great for learning and testing ideas.

To start the Python shell, open Terminal and type:

python3

You'll see the Python prompt (>>>). Try a simple command:

print("Hello, Raspberry Pi!")

Press Enter to see the output. Type exit() or press Ctrl + D to leave the Python shell.

Python scripts

For anything beyond simple experiments, you'll write Python code in files (called scripts) and run them. Python script files end with .py.

Choose an IDE

While you can write Python in any text editor, using an Integrated Development Environment (IDE) makes coding much easier. An IDE provides syntax highlighting, error detection, and debugging tools.

Raspberry Pi OS includes two Python IDEs by default:

Thonny (recommended for beginners)

Thonny is designed for beginners and comes pre-installed on Raspberry Pi OS. It has a clean interface, built-in Python shell, and excellent debugging features.

To open Thonny:

  • Click the Raspberry Pi menu → Programming → Thonny Python IDE
  • Or run thonny from Terminal

Thonny shows your code in one pane and the Python shell in another, making it easy to write code and test it immediately.

IDLE (Python's official IDE)

IDLE is Python's built-in IDE and offers a straightforward coding environment.

To open IDLE:

  • Click the Raspberry Pi menu → Programming → Python 3 (IDLE)
  • Or run idle3 from Terminal

Geany (for multiple languages)

Geany is a lightweight text editor and IDE that supports many programming languages, not just Python. It's also pre-installed on Raspberry Pi OS.

To open Geany:

  • Click the Raspberry Pi menu → Programming → Geany
  • Or run geany from Terminal

Choose the IDE you find most comfortable. For beginners, Thonny is the best starting point.

Run Python scripts

Once you've written a Python script and saved it (for example, hello_pi.py), you can run it from Terminal:

python3 /path/to/your/script/hello_pi.py

For example, if you saved hello_pi.py in your home directory's Documents folder:

python3 ~/Documents/hello_pi.py

The ~ symbol is shorthand for your home directory (/home/pi by default).

Your first Python script

Try creating a simple script to test everything works:

  1. Open Thonny or IDLE.
  2. Type this code:
    print("Hello from Raspberry Pi!")
    print("Python version:", __import__('sys').version)
  3. Save the file as test.py in your home directory.
  4. Run it from Terminal:
    python3 ~/test.py

You should see output showing your message and Python version.

If it doesn't work

  • python3 command not found: Ensure you're using python3 (with the 3) rather than just python. On Raspberry Pi OS, python3 is the correct command. If Python is genuinely missing, reinstall Raspberry Pi OS or use sudo apt install python3.
  • Permission denied when running a script: Make sure you're using python3 script.py rather than trying to run the script directly. Alternatively, add execute permissions: chmod +x script.py, then add #!/usr/bin/env python3 as the first line of your script.
  • Module not found errors: If your script uses external libraries (modules not included with Python), install them with pip3 install module-name. For example: pip3 install numpy.
  • IDE won't open: Try running it from Terminal (thonny or idle3) to see any error messages. Ensure your Raspberry Pi OS is fully updated: sudo apt update && sudo apt upgrade.

Need help with Python?

Tell us what you're trying to do, any error messages you see, and which IDE or method you're using.

Contact Vilros Support ↗