Raspberry Pi 3 Raspbian: Running Python from the command line

In this tutorial we will check how to print a very simple “Hello World” message with Python running on the command line, on the Raspberry Pi. This tutorial was tested on a Raspberry Pi 3 model B+.


Introduction

In this tutorial we will check how to print a very simple “Hello World” message with Python running on the command line, on the Raspberry Pi. If you want to learn how to print a “Hello World” message using IDLE, the Python IDE, please check this previous article.

As mentioned in the previous tutorial, some versions of the Raspbian operating system have two versions of Python installed (Python 2.x and Python 3.x, where x is the minor version). You can check more about Python versioning here.

For this tutorial, I’m running version 4.9 of Raspbian, installed using NOOBS, which includes both versions of Python.

So, we are going to check how to run the “Hello World” printing program from the command line in the two versions of Python.

In both cases, the first thing we will need to do is opening the corresponding Python interpreter and then sending the command to print the message.

This tutorial was tested on a Raspberry Pi 3 model B+.


Running on Python 2.x

As mentioned, we will need to open the Python interpreter to get started. To do it, simply open a command line and type the following command:

python

After sending the command, it should start the interpreter, a shown in figure 1. Note that, as highlighted, the interpreter lists the whole Python version, including the minor and the micro version.

Raspberry Pi Python 2.7 interpreter

Figure 1 – Starting the Python 2.x interpreter via command line.

Now to print a “Hello World” message, simply type the following command and hit enter (you can change the string inside the double quotes to print whatever message you want):

print("Hello World!")

The message should get printed as illustrated in figure 2.

Raspberry Pi 2.7 Python Hello World from command line

Figure 2 – Python 2.x “Hello World” program.

You can keep exploring other Python commands in the prompt. When you finish, simply close the whole command line.


Running on Python 3.x

As we have seen in the previous section, if we just write the python command on the command line, it will open the Python 2.x interpreter. So, to open the Python 3.x interpreter, our command needs to reflect that.

After opening a new command line, simply type the following command and hit enter (notice the 3 at the end of the command):

python3

As shown in figure 3, now it will open the Python 3 interpreter. You can confirm that by the version highlighted in the figure.

Raspberry Pi Python 3.5 interpreter

Figure 3 – Starting the Python 3.x interpreter via command line.

To print the “Hello World” message, simply send the same Python command we have used before.

print("Hello World!")

You should get the same result, as illustrated in figure 4.

Raspberry Pi 3.5 Python Hello World from command line.png

Figure 4 – Python 3.x “Hello World” program.

 

3 thoughts on “Raspberry Pi 3 Raspbian: Running Python from the command line”

  1. Pingback: Raspberry Pi 3 Raspbian: Running Python scripts on IDLE | techtutorialsx

  2. Pingback: Raspberry Pi 3 Raspbian: Running Python scripts on IDLE | techtutorialsx

  3. Pingback: Raspberry Pi 3 Raspbian: Running a Flask server | techtutorialsx

  4. Pingback: Raspberry Pi 3 Raspbian: Running a Flask server | techtutorialsx

  5. Pingback: Raspberry Pi 3 Raspbian: Exposing a Flask server to the local network | techtutorialsx

  6. Pingback: Raspberry Pi 3 Raspbian: Exposing a Flask server to the local network | techtutorialsx

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading