Running Python Programs from the Terminal on Raspberry Pi

Running Python programs directly from the terminal is one of the most efficient ways to execute your scripts on a Raspberry Pi. Whether you’re automating tasks, working on projects, or testing code, learning how to run Python programs via the terminal is essential for every Raspberry Pi user. This guide will show you how to Running Python Programs from the Terminal on Raspberry Pi, with simple and beginner-friendly steps.

Why Run Python Programs from the Terminal?

  • Efficient Testing: You can quickly run your Python scripts without needing a full Integrated Development Environment (IDE).
  • Automating Tasks: Ideal for running automation scripts, like controlling hardware, sensors, or networking on Raspberry Pi.
  • Remote Access: You can run Python scripts remotely via SSH or other command-line interfaces without needing a graphical interface.

Basic Steps for Running Python Programs from the Terminal

Here’s a step-by-step guide to running Python programs directly from the terminal.

1. Writing Your Python Script

First, you need to create a Python script. You can use a text editor like nano, Thonny, or any other IDE to write the script.

Example: A Simple Python Script

Let’s create a simple Python script that prints “Hello, World!”.

  1. Open the terminal on your Raspberry Pi.
  2. Create a new file called hello.py using a text editor (we’ll use nano in this example):
    • Command:
      nano hello.py

In the file, write the following Python code:
print(“Hello, World!”)

  1. Save the file by pressing Ctrl + X, then Y, and hit Enter to confirm.

2. Running a Python Script with Python 3

Once your script is saved, you can run it from the terminal. On modern Raspberry Pi OS, Python 3 is the default version, so we’ll focus on that.

Running the Python Script

  • Command:
    python3 hello.py

After running this command, you should see the following output:

Hello, World!

This indicates that your Python script has successfully executed.

3. Making a Python Script Executable

If you want to run your Python script without explicitly typing python3 every time, you can make it executable. This is especially useful for automation scripts.

Step 1: Add the Shebang Line

The shebang line tells the system which interpreter to use to run the script. Open your Python script and add this line at the very top:

#!/usr/bin/env python3

his ensures the script will be executed using Python 3.

Step 2: Make the Script Executable

You can make your Python script executable by using the chmod command.

  • Command:
    chmod +x hello.py

Step 3: Run the Script Without python3

Now, you can run your script directly from the terminal without needing to type python3 before the filename.

  • Command:
    ./hello.py

This will execute the hello.py script and produce the same output:

Hello, World !

4. Running Python Programs in the Background

Sometimes, you might want to run your Python program in the background so that the terminal remains free for other tasks. You can do this using the & symbol.

  • Command:
    python3 hello.py &

This will run the Python script in the background.

Viewing Background Processes

To see the list of background processes, you can use the jobs command.

  • Command:
    jobs

To bring the background process back to the foreground, use fg.

5. Running Python Scripts on Boot

You can also configure your Raspberry Pi to run Python scripts automatically when it boots up. This is useful for automation tasks, like running a script to control sensors, LEDs, or other hardware components.

Using Crontab to Run Python on Boot

  1. Open the crontab configuration file for the current user:
    • Command:
      crontab -e

Add the following line at the end of the file to run the Python script on boot:
@reboot /usr/bin/python3 /home/pi/hello.py &

  1. Save and exit the editor. The script will now run automatically whenever the Raspberry Pi boots.

6. Running Python Scripts Remotely via SSH

If you’re accessing your Raspberry Pi remotely using SSH, you can still run Python scripts through the terminal. Simply SSH into your Raspberry Pi and run the script as you normally would.

  • Command (SSH into the Raspberry Pi):
    ssh pi@raspberrypi
  • Command (Running the Python script):
    python3 hello.py

This is especially useful for headless setups where the Raspberry Pi doesn’t have a monitor attached.

Real-World Example: Running a Python Script to Control an LED

Let’s say you’re controlling an LED connected to your Raspberry Pi’s GPIO pins. You’ve written a Python script that blinks the LED. Here’s how you would run it:

  1. Write the Script: Create a Python script called blink.py that controls the LED using the GPIO library.
  2. Run the Script:
    python3 blink.py
  3. Make it Executable (optional):
    chmod +x blink.py
  4. Run the Script on Boot (optional): Use crontab to run the script automatically whenever the Raspberry Pi starts.

FAQ: Running Python Programs from the Terminal on Raspberry Pi

Q: How do I run Python 2 programs on Raspberry Pi?
A: If you need to run a Python 2 program, use the python command instead of python3.
Command: python hello.py.

Q: Why does my Python script throw a “Permission Denied” error?
A: This error occurs if the script doesn’t have execute permissions. You can fix this by running:
chmod +x script_name.py.

Q: Can I run Python scripts automatically when Raspberry Pi boots up?
A: Yes, you can use crontab or add your script to the rc.local file to run it at boot. Use the @reboot option in crontab for this.

Conclusion:

By learning how to run Python programs from the terminal on Raspberry Pi, you can execute scripts efficiently, automate tasks, and manage your projects with ease. Whether you’re running a simple script, executing programs in the background, or configuring your Raspberry Pi to run Python code at startup, the terminal provides a powerful and flexible way to manage Python programs on Raspberry Pi.