How to Run a Python Script in Red Hat Enterprise Linux 9?

How to Run a Python Script in Red Hat Enterprise Linux 9

Python is a general-purpose programming language written by Guido Van Rossum in 1991. It is used in web development to create applications and in system administration for task automation. At the time of writing this article, the most recent version of Python is 3.

It is cross-platform and can run on Windows, Linux/Unix, macOS, and Android.

In this guide, we are elaborating on the following four methods to run a Python script in Red Hat Enterprise Linux 9.

  • How to run a Python script from a terminal?
  • How to run a Python script from IDE?
  • How to run a Python script in an interactive mode?
  • How to run a Python script from Jupyter Notebook?

How to Write a Python script

For the sake of understanding, we will write a hello world program in Python.

Suppose we want to create a Python script in hello.py in current working directory in Nano editor.

Open the terminal and run the following command.

nano hello.py

Write the following code in the file:

print (“Hello World!”)

Press CTRL + x and save the file when you are asked.

To verify if the script file has been created, list the content of your working directory.

ls

How to run a Python script from the terminal?

Open up the terminal from the activities menu. Execute the following command to run the hello Python script.

python3 hello.py

As you can see from the output, the Hello World! Is displayed on the terminal.

How to run a Python script from Thonny IDE?

You can run a Python script from Thonny IDE. Open Thonny program from the terminal or activities menu. Click CTRL + o to open the hello.py program we have written earlier.

Click F5 from the keyboard or click the play button to run the program.

An output Hello World! Will be displayed on the shell.

You can use other IDEs such as PyCharm and JetBrains to run the Python script.

How to run a Python script in an interactive mode?

You can run a Python script in an interactive mode as follows. Type the following on the terminal:

python3

This will get you in an interactive mode where you can type and execute the Python scripts one command at a time.

Suppose you want to display Hello World! on the terminal, write:

print (“Hello World!”)

As soon as you Enter from the keyboard, Hello World! Will be displayed on the terminal.

Let’s take an input as the name of a person from the user and display it on the terminal.

name = input (“What is your name” )
print (“Your name is: ” + name)

To exit from the interactive mode, run:

exit()

How to run a Python script in Jupyter Notebook

Jupyter Notebook is a browser-based IDE. You can write, interpret, and run the program in the browser without installing any IDE on the system. To get started open the link in your browser and click ‘try’ from the menu bar.

Scroll down and click Jupyter Notebook.

From the File, click New -> Notebook.

From the popup menu, select Python(Pyodide)

Write the program:

print (“Hello World!”)

Click the play button or press SHIFT + Enter from the keyboard to run the program.

Conclusion

We are now concluding this article. You can use any of the above methods to run your Python script on your system. Please leave your feedback to help us improve this article. Keep visiting Linux World.