Raspberry Pi – Run Python Script in the Terminal

Great, you’ve learned a bit of Python 3 with your Raspberry Pi and you might be wondering: how can you run a Python 3 script in the terminal?

For now you may have used only the Thonny Python IDE, or any other easy-to-use system to write Python programs. This is perfectly fine, especially to start learning. But after a certain point you’ll need to work with Python directly inside the terminal of your Raspberry Pi.

In this tutorial I’ll show you how to do that. Let’s get started!

Check Python version on your Raspberry Pi

As you may now, Python 2 is not supported anymore since 2020. However, this doesn’t mean that Python 2 has disappeared, or that it can’t work! In many systems Python 2 is still present, and the first thing you want to check is if it’s still there.

If you have Python 2 and Python 3 installed on environment, then for the following you’ll want to make sure you only run scripts using Python 3, which is the newer and recommended version to use.

Open a terminal, and run:

You are learning how to use Raspberry Pi to build your own projects?

Check out Raspberry Pi For Beginners and learn step by step.

$ python --version
Python 3.9.2

If you see something starting with “Python 3.x”, then everything is fine. Whether you type “python” or “python3”, you’ll use the correct version. You can skip the following and jump to the next part of the tutorial.

But if you see something starting with “Python 2.x”, you’ll have to manually type “python3” to be able to use Python 3. For example:

$ python3 --version
Python 3.9.2

If you still have Python 2 and just want to run Python 3 by default and not pay attention to the version anymore, you can add an alias in your bashrc. This will make sure that when you type “python”,  “python3” will be executed instead. To do that simply run this command: echo "alias python=python3" >> ~/.bashrc.

Note: for the following of this tutorial I’ll still use the “python3” command. If you already have Python 3 by default, you can choose to use “python” instead.

Run Python code directly on the terminal

Before we even begin to write and execute complete files, you can just run any Python command you want directly on the terminal – in what we call a “Python shell”.

If you’ve used Thonny IDE before, you could use the shell panel to write + execute code directly, line by line. Well this is the same.

Open a terminal, and run “python3” without any argument.

$ python3
Python 3.9.2 (default, 13:03:44) 
[GCC 10.2.1] on linux
Type "help", "copyright", "credits" or "license" for more information.

This is what you should get at the end : “>>>”.

This means that you’re in a Python shell, inside your “terminal shell”. Now, all the commands you’ll write will be interpreted as Python commands.

>>> print("Hello")
>>> a = 2
>>> print(a)
>>> import os

As you can see, we can even create variables and use them later.

Using the Python shell in the terminal is a great way to quickly test small features, for example:

  • if a module is correctly installed: running import module will give you an error if the module can’t be found.
  • to test small hardware components with the “RPi.GPIO” module. You can power on/off a LED directly from the Raspberry Pi terminal.
  • etc.

Now, how to quit this shell?

If you press CTRL+C you’ll get this:


Python will catch the signal sent by CTRL+C and will throw a KeyboardInterrupt exception, which you can also catch in your code if you want to.

But the shell won’t be closed. To close the shell, you’ll have to execute the command “exit()” – with the parenthesis.

>>> exit()
pi@raspberrypi:~ $

Note that when you exit the shell, all variable states and functions you’ve written inside the shell will be lost. So, only use the shell for quick tests, and when you want to create real programs, create your own Python scripts instead.

Create and run Python scripts on Raspberry Pi

If you’ve used the Thonny IDE (or any other IDE) before on the Raspberry Pi, you’ve seen that you just need to:

  1. Open the IDE and write Python code in the text editor.
  2. Save the script into a file thanks to the graphical interface.
  3. Execute the script by clicking on the “play” button.

Good news: you can do all those steps without even leaving the terminal on your Raspberry Pi!

>> Video version:

After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!

Write a Python program inside the terminal

To do that, you’ll need to use a text editor.

On Raspberry Pi OS and most other operating systems, you can easily find and use the Nano text editor. When you open a file with Nano, no new window will be open. You’ll just write some text in the terminal, and then save the file if you want to. Another popular IDE for Raspberry Pi is Vim.

To create and write a new Python script with Nano:

  • Open a terminal.
  • Use nano filename.py on an existing file or just give a new file name (you can save it later).
  • Write your Python code.
  • Press CTRL+S to save the file.
  • press CTRL+X to exit Nano and come back to the terminal.

If you want to create a file first, before editing the text, use the “touch” command: touch filename.py. You can also modify a file name with “mv”: mv old_file_name.py new_file_name.py. And to remove a file: rm filename.py.

Note: when creating Python files in the terminal, it’s important to give them the extension “.py”, so you can differentiate them from other types of files.

Also, as a best practice, add #!/usr/bin/env python3 at the beginning of your Python scripts. This will make sure that any program running this script will know which interpreter (here Python 3) to use.

Well, as you can see, it’s fairly easy and quite convenient to use the terminal to write a Python script.

Run a Python script in the terminal of your Raspberry Pi

All right, now that you have a Python script saved into a file, it’s time to run it directly from the terminal.

Simply use “python3” + the name of the file: python3 filename.py. This will execute the script just like if you’d execute it inside an IDE.

Now, to stop/kill the script, you’ll have to press CTRL+C.

You’ll notice that when you press CTRL+C, you get a “KeyboardInterrupt” message. It’s an exception you can actually catch in your program. For example, after catching KeyboardInterrupt you could clean up some hardware pins or closing some open files, before exiting the program.

Also, a best practice is to make your script an executable. To do that, run “chmod +x” on the file: chmod +x filename.py. This way, you can directly run your script without using the “python3” command line tool: ./your_script_with_or_without_an_extension.

Manage Python packages with pip3

>> Video version:

If you create and run your Python scripts in the terminal, you’d also want to be able to manage Python modules from the terminal as well.

This is also quite easy thanks to the pip3 command line tool.

And here again, similar to what we’ve seen with “python” and “python3” commands, there are 2 variations for pip.

$ pip --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)
$ pip3 --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

If you get the same for both “pip” and “pip3”, then it means that by default pip3 is used.

But if you get an error with pip3 because your Python version is still 2, you need to install it: sudo apt install python3-pip. Then, add an alias in your bashrc to use pip3 instead: echo "alias pip=pip3" >> ~/.bashrc.

Now, to see the list of all Python 3 modules, run pip3 list. For each module you’ll also get the installed version.

If you want to filter the results, add “grep”:

$ pip3 list | grep "camera"
picamera          1.13
picamera2        0.3.9

To install a new module, run pip3 install module_name. And to remove it, pip3 uninstall module_name.

Once you’ve installed a module with pip3 in the terminal, you can easily check if the module can be found: open a Python shell and try to import the module. In 10 seconds you’ll know if things are working.

Conclusion – running a Python script from the terminal in your Raspberry Pi

In this tutorial you’ve seen how to configure your Raspberry Pi so that you can run a Python script from the terminal.

When you start with Raspberry Pi and Python 3, it’s good to use the Thonny Python IDE at first, which will allow you to focus on learning Python. Then, using the terminal is a mandatory step if you want to get a better understanding and go further.

And in the end, once you know how to use Python inside the terminal, it becomes a matter of preference. Some developers prefer using only the terminal, others prefer using IDEs to develop. This is your choice to make!

Did you find this tutorial useful?

Do you want to learn how to build awesome projects with Raspberry Pi?

If yes, this course is for you:

Raspberry Pi For Beginners - Complete Course

>> Raspberry Pi For Beginners <<