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:
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:
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:
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:
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:
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
Example 1
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:
>>> 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
Example
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:
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
Example
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.