Raspberry Pi

Run Python Script on Terminal – Raspberry Pi

If you love Python you might have used Thonny Editor to run Python codes on Raspberry Pi. As a beginner, it is okay to run Python on a specified Python editor. But if you are well familiar with Raspberry Pi OS and want to use the terminal for all kinds of programming on Raspberry Pi then in this article, we will show you how to run a Python script on the terminal.

Run Python Script on Terminal

To run Python on Raspberry Pi, launch the terminal first and then follow the below-mentioned steps:

Step 1: Ensuring Pip Installation

Pip is used to install Python packages, type the below-mentioned command to ensure that pip is installed in Raspberry Pi:

$ pip --version

As a result of the above command the version of pip will display on the screen.

If the pip is not installed you can follow this article.

You can also check the version of pip3 by using the below-mentioned command:

$ pip3 --version

As a result of the above command the version of pip3 will display on the screen:

Step 2: Python Version

Now finally to ensure that Python is installed, use the below-mentioned version command:

$ python --version

As a result of the above command, it will display the installed version of Python.

You can also check the version of Python 3 by using the below-mentioned command:

$ python3 --version

In this article we will run all our commands using Python3.

Step 3: Running Python

Now finally it is time to run Python3 so we can write our Python script on the terminal. To run Python3 use the below-mentioned command:

$ python3

As a result, the Python3 package will run and “>>>” will display on the screen. The Python script can now be written after these “>>>”.

Step 4: Writing Python Script

The most basic command that can be run in any programming language is the print command, so here we will print a message on screen by using the Python script:

Syntax

>>> print(“message”)

Example 1

>>> print(“My name is Linux hint”)

After entering the above command, the message will be printed on the screen.

Example 2

Now let’s perform some mathematical operations using Python script:

>>> z=36 //declared 1st variable

>>> x= 5 //declared 2nd variable

>>> y=z+x //performing addition here and storing the value inside the variable

>>> print(y) //displaying the output variable

As the result, the output of above calculations will display on screen:

Creating a Separate Python File on Terminal

The other method to run the Python script using the terminal is to create a Python file using the below command:

Syntax

$ nano <filename>.py

Example

$ nano my_pythonfile.py

As a result of the above command, a new file will be created on the terminal. Where you can type your required Python code or you can also copy the below Python code:

k=12

u=10

m=k-u

print (m)

Then save the file by pressing keys Ctrl + X and then Y. Finally press Enter to get back to the terminal.

Now to run the code type the below-mentioned Python3 command:

Syntax

$ python3 <filename>.py

Example

$ python3 my_pythonfile.py

As a result, the output of the code will be displayed on the terminal.

Conclusion

Running a Python script on Raspberry Pi is not a very difficult task; you are required to have a pip which is a required package to install Python packages. To run Python firstly type the Python3 command and after this, you can type the Python commands and each command is run by pressing the Enter button. To save the Python code to a separate file nano command can be used to create a Python file. Then to run the file, use the python3 command along with the filename.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.