Python is a powerful programming language that can be used for everything from web development to data research. If you use Ubuntu and want to run Python code from the command line, this comprehensive article will bring you through the procedure step by step.

Running Python code in the Ubuntu command line is a fundamental skill for any programmer or developer. Python’s flexibility and power and Ubuntu’s command line interface along with Ubuntu’s command line interface, allow you to efficiently write, execute, and debug Python programs. It enables you to enjoy Python’s full capability without the requirement for a graphical user interface.

What is Ubuntu?

Ubuntu is a free and open-source operating system built on the Linux kernel. It is one of the most popular Linux distributions, designed to be user-friendly and accessible to a wide range of users. Canonical Ltd. and a global volunteer community work together to create Ubuntu.

Ubuntu offers a complete desktop environment with a graphical user interface, making it appropriate for both novice and experienced users. It comes with a variety of pre-installed software, including a web browser, office productivity suite, media players, and others. Furthermore, Ubuntu includes a big software repository that users may use to effortlessly install and update additional software packages. Canonical updates Ubuntu every six months, with long-term support (LTS) versions provided every two years.

Ubuntu has grown in popularity because of its ease of use, community support, and adherence to open-source software principles. Individuals, educational institutions, enterprises, and organizations all around the world utilize it for a variety of reasons, including desktop computers, servers, and cloud infrastructure.

Prerequisites To Run Python Code in Ubuntu Command Line

Before we get started with the procedures for running Python code in Ubuntu command, make sure you have the following prerequisites installed on your system:

  1. Python Setup:

Verify that Python is installed on your Ubuntu computer. Open the terminal and type “python3 –version” to check the installed Python version.

If you haven’t already installed Python, you can easily do so by typing the command for the package manager apt.

sudo apt update

sudo apt install python3

Select a text editor of your preference to write your Python code. You can use a variety of text editors, including Notepad, Sublime Text, Atom, Visual Studio Code, etc. Install your preferred text editor if it is not already available on your system.

After setting up our prerequisites, let’s proceed to run Python code from the Ubuntu command line.

How to Run Python Code in Ubuntu Command Line?

1. Launching the Terminal

To start, launch the terminal by clicking on the terminal icon in the Ubuntu launcher. 


On your keyboard, hit Ctrl + Alt + T. This is the most commonly used keyboard shortcut in Ubuntu to launch the Terminal.

2. Writing Python Code

Using any text editor or integrated development environment (IDE) of your choice create a new Python file with the .py extension.

For example, you can run the command “nano”

to create a new file named in the Nano text editor. When the file is open, you can start writing your Python code.

3. Saving the Python File

After writing your Python code, it is essential to save the file before executing it. To save the file in Nano, press Ctrl+O, enter the desired file name, and hit Enter. Then press Ctrl + X to leave Nano.

4. Executing Python Code


The command python3 is used to execute Python3 files.

5. Analysing the Results

If your Python code generates any output, it will be shown in the terminal after the script has been run. To make sure your code is operating properly and producing the desired outcomes, you can check the output.

6. Debugging Python Code

Debugging is an important aspect of the software development process, especially in Python programming. When facing issues or unexpected behavior in your Python code, you can follow these steps to debug and identify the problem:

Advanced Applications

Certainly! Running Python code from the Ubuntu command line gives various advanced options and features that might improve your programming experience. Here are a few examples:

For example, Consider the following Python script, “,” which receives two command line arguments and prints them:

import sys

arg1 = sys.argv[1]

arg2 = sys.argv[2]

print("Argument 1:", arg1)

print("Argument 2:", arg2)

To run this script with command line arguments, use the following command:

python3 value1 value2

Common Errors and Troubleshooting

When running Python code from the Ubuntu command line, you may meet some frequent difficulties and issues. Understanding these traps and how to avoid them might help you save time and effort during development.

Here are a few examples of common errors and their solutions:

Syntax errors occur when you write code that does not follow the Python language rules. These faults are frequently highlighted with a specific error message that shows the line and location of the error.

To fix syntax errors, carefully review your code and check that you have used the correct syntax, such as proper punctuation, correct use of parentheses, and valid variable names.

Module not found issues occur when you attempt to import a module that is not installed or available in your Python environment.

To resolve module not found issues, ensure that you have installed the appropriate module using pip. Check the spelling and case of the module name to ensure it matches the import statement.

When your Python program encounters an error that it is unable to manage, an unhandled exception occurs. These errors can cause your program to crash or act abnormally.

Use tries and except statements to catch and manage specific types of mistakes when dealing with exceptions. This enables you to handle mistakes carefully and provide suitable feedback to the user.

Python has several versions, and some libraries may not be compatible with all of them. If you’re using a library that is not compatible with your Python version, you might face compatibility issues.

To overcome version compatibility concerns, ensure that you are using a compatible version of Python for your project. Check the library’s documentation to determine which Python versions are supported.

If your script requires internet connectivity, make sure your system has an active internet connection. Some Python libraries or packages may need the installation of additional dependencies or the use of online resources.

FAQs on How to run Python code in Ubuntu command line?

Question: What is a terminal in Python?

Answer: The term “terminal” in Python refers to a text-based interface or command-line environment where you can interact with the Python interpreter or run Python scripts. It allows you to run Python code, access system resources, and perform numerous operations using text commands.

Question: How to create a Python script?

Answer: To create a Python script, perform these two steps:

Question: Can I use Python for web development in Ubuntu?

Answer: Yes, Python is commonly used for web development, and various frameworks for building web applications in Python are available, including Django, Flask, and Pyramid. These frameworks provide the tools and resources required to create strong and scalable web applications.

Question: How to run Python code in Ubuntu command line?

Answer: To run Python code from the command line on Ubuntu, open the terminal and type “python3” to execute the Python file.

Question: How to compile Ubuntu source code?

Answer: Compiling the Ubuntu source code is a complicated process that cannot be summed up in two lines. Obtaining the source code, installing build dependencies, establishing the development environment, and creating the source code using various commands are all part of the process. It necessitates extensive knowledge of and familiarity with the Ubuntu build system.