Python is a powerful programming language, and it has become a very popular language after its version 2 release. It is frequently used in Data Science, AI, and Machine Learning. Python has a wide range of applications due to the availability of many libraries.

In this article, we will learn how you can write and execute Python code on Ubuntu 22.04.

How do I run a Python file in Ubuntu 22.04?

If you are using Ubuntu 22.04, you can write the code of your program in any text editor like the nano, or gedit. But if you want to execute the code, then you must have Python installed on your Ubuntu system. Follow the steps, mentioned below to run a Python file in Ubuntu 22.04. 

Step 1: Check the Python Version

Before running a Python file in Ubuntu 22.04, first check the Python version. You can check the Python version by using the following command:

$ python3 -version

The command will display the version of Python installed on the computer system:

If the Python is not installed on your system, you can follow the article for installing the Python.

Step 2: Write the Python Code

If you want to execute a Python file, firstly we must have a code file. Open any text editor to write the code. Use the following command to make a Python 3 code file:

$ sudo nano “filename.py”

The command will open the file in the nano text editor:

Now you can write the code like:

# profile
name = "John Doe"
age = 46

print("name is:", name)
print("age of:", name, "is", age)

Save the file by pressing “ctrl + o” and exit the file by using “ctrl+x”.

Step 3: Run the Python Code

In this step, we run the Python code file. If you want to run Python code execute the following command:

$ python3 “filename.py”

The command will execute the Python code and display the output:

You can also run the Python code by using the Python interpreter which comes with the default installation of the Python. When we install Python, the Python interpreter is also installed. You can use this interpreter for the execution of Python code.

Use the following command to launch the Python3 interpreter:

$ pyhton3

The command will open the Python interpreter shell:

Now you can write and execute the Python code as follows:

If you want to exit the Python3 interpreter shell, use the exit() function:

This is how you can run a Python file in Ubuntu 22.04. 

Conclusion

To run a Python file in Ubuntu 22.04, you can use the command “python3 <filename.py>” in the terminal. You can also use Python shell for executing the code. In this article, we have explained how you can run the Python file in Ubuntu 22.04.