Raspberry Pi

Can I Program a Raspberry Pi with Python Over SSH?

Python is a versatile and widely used programming language for developing various useful applications in the field of robotics, IoT, Big Data, and Machine Learning. People mostly used this programming language to develop websites, applications, automate tasks and perform data analysis. This language is the official language of the Raspberry Pi system, thus, it’s fair to say that it plays an important part for the users using the Raspberry Pi OS GUI version on their device. However, the question needs to be asked whether there is a possibility to program the Raspberry Pi with Python over SSH. The reason is most people preferred accessing the Raspberry Pi terminal over SSH.

This article is a detailed guide for those users who want to learn Python on Raspberry Pi through SSH.

Can I Program a Raspberry Pi with Python Over SSH?

Yes, you can program a Raspberry Pi with Python over SSH. The steps to program Raspberry Pi with Python over SSH are mentioned below.

Step 1: Enable SSH on Raspberry Pi

The first step is to enable SSH to access the Raspberry Pi system remotely so that it can be programmed using SSH. To enable SSH, open the Raspberry Pi Configuration tool using the below-mentioned command:

$ sudo raspi-config

From the configuration tool window, select the “Interface Options”.

Then select the “SSH” option:

Apply “Yes” to enable SSH on Raspberry Pi.

A prompt will appear to inform that SSH is enabled, click “Ok”.

Now you can access the Raspberry Pi through SSH on your PC, read the article for guidance.

Step 2: Check the Version of Python

After accessing the Raspberry Pi through SSH onto the PC, you must ensure that python is installed in it (Raspberry Pi system) and for that follow the below-mentioned command:

$ python3 --version

The output will display the installed version of Python:

If somehow, you are having a problem, you can reinstall python using the following command:

$ sudo apt install python3

Step 3: Write and Run Python Program

Now finally you can write a python program. The two methods to write and run python programs on Raspberry Pi through SSH are mentioned below:

Method 1: Make a Python File

The first way is to make a Python file using the nano editor through the below-mentioned command:

Syntax

$ nano <file name>.py

Example

$ nano pythonfile.py

Now you can type any of the Python code in this file. Here, I have used a very basic python multiplication code just to give you an example.

x=2

y=10

z=x*y

print (z)

Then hit keys Ctrl+X and then Y to save and exit the file.

To run the file, just use the below-mentioned command and the output of the program will be displayed on the terminal:

Syntax

$ python3 <file-name>.py

Example

$ python3 pythonfile.py

Method 2: Directly Write Python Script

The other method is that you can directly run the Python script on the SSH terminal instead of making a separate file. Just use the below-mentioned command to open the python over the SSH terminal:

$ python3

Now you can write any of the python programs directly onto the SSH terminal by adding the line one by one given below:

x=2

y=10

z=x*y

print (z)

In this way, you can write multiple codes in Python and run them on Raspberry Pi over SSH.

Conclusion

Yes, Python can be used to Program Raspberry Pi over SSH. Ensure that the SSH is enabled and Python is installed on your Raspberry Pi system. After accessing the Raspberry system over SSH, you can make a separate python code file using the nano editor or run the python interpreter and add the code one by one to run it directly onto the terminal.

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.