Home Blog Blog Details

How to use Python in Raspberry Pi?

January 22 2025
Ampheo 7

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
If you haven’t already, install Raspberry Pi OS (previously Raspbian) on your Raspberry Pi. You can download it from the official Raspberry Pi website and install it using the Raspberry Pi Imager.

To use Python on a Raspberry Pi, follow these steps:

1. Install Raspberry Pi OS

If you haven’t already, install Raspberry Pi OS (previously Raspbian) on your Raspberry Pi. You can download it from the official Raspberry Pi website and install it using the Raspberry Pi Imager.

2. Ensure Python is Installed

Raspberry Pi OS comes with Python pre-installed. You can check the version of Python installed by running the following command in the terminal:

bash
 
python3 --version

It should display something like:

 
 
Python 3.x.x

3. Using Python in the Terminal

You can start an interactive Python shell by typing:

bash
 
python3

This will launch the Python shell where you can start writing and testing Python code interactively.

For example:

python
 
>>> print("Hello, Raspberry Pi!")

4. Creating and Running Python Scripts

To create a Python script:

  1. Open a text editor (e.g., Nano, Thonny, or Geany).
  2. Write your Python code. For instance:
     
    python
     
    # hello.py print("Hello, Raspberry Pi!")
     
  3. Save the file with a .py extension.

To run the script, use the following command in the terminal:

bash
 
python3 hello.py

5. Using Thonny IDE (Pre-installed)

Thonny is a Python IDE that comes pre-installed with Raspberry Pi OS, making it easy to write, test, and debug Python code.

  • To open Thonny, go to the Start Menu -> Programming -> Thonny Python IDE.
  • Write your Python code in Thonny and run it by clicking the green Run button.

6. Installing Python Libraries

You can install additional Python libraries using pip, the Python package manager. For example, to install the requests library:

bash
 
pip3 install requests

You can use this command for any Python library you need to install.

7. Accessing GPIO Pins (Raspberry Pi Specific)

The Raspberry Pi has GPIO pins that you can control with Python. You can use the RPi.GPIO library to interface with these pins. Here’s a simple example:

python
 
import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BCM) # Use Broadcom pin-numbering scheme
GPIO.setup(17, GPIO.OUT) # Set GPIO pin 17 as an output
 
while True:
GPIO.output(17, GPIO.HIGH) # Turn on LED
time.sleep(1) # Wait for 1 second
GPIO.output(17, GPIO.LOW) # Turn off LED
time.sleep(1) # Wait for 1 second

To run this script, you'll need to have RPi.GPIO installed, which can usually be done with:

bash
 
sudo apt-get install python3-rpi.gpio

8. Running Python at Boot (Optional)

If you want your Python script to run automatically when the Raspberry Pi starts up, you can add it to the rc.local file or create a cron job. Here’s how to do it with rc.local:

  • Edit the rc.local file:
     
    bash
     
    sudo nano /etc/rc.local
     
  • Before the exit 0 line, add:
     
    bash
     
    python3 /path/to/your/script.py &

9. Remote Access (Optional)

You can also access your Raspberry Pi remotely via SSH if you're working on a headless setup. You can enable SSH from the Raspberry Pi Configuration tool and use an SSH client like PuTTY to connect from another computer.

Conclusion

With these steps, you should be able to easily start programming in Python on your Raspberry Pi, whether you're building simple scripts or more complex applications that interact with hardware.

Ampheo