How To Run Python Code In Ubuntu Command Line


Running Python code in the Ubuntu command line is a fundamental skill for any developer. Whether you’re a beginner or an experienced programmer, the command line offers a quick and efficient way to run Python scripts and interact with your code. In this article, I’ll guide you through the process of running Python code in the Ubuntu command line, sharing some of my personal tips and tricks along the way.

Checking Python Installation

Before we dive into running Python code, it’s essential to ensure that Python is installed on your Ubuntu system. Open the terminal and type the following command:

python --version

If Python is installed, you will see the version number displayed. If it’s not installed, you can install it using the following command:

sudo apt update
sudo apt install python3

Creating and Running Python Scripts

Once Python is installed, you can create and run Python scripts directly from the command line. Using your favorite text editor, create a new Python script, for example,, with the following content:

print("Hello, Ubuntu command line!")

To run the script, navigate to the directory where the script is saved using the cd command. Then, run the script using the following command:


You should see the output “Hello, Ubuntu command line!” displayed in the terminal. It’s that simple!

Virtual Environments

Virtual environments are a great way to manage dependencies and isolate project environments. To create a virtual environment, use the following commands:

python -m venv myenv
source myenv/bin/activate

After activating the virtual environment, you can install packages and run Python scripts without affecting the system-wide Python installation.

Python Interactive Shell

The Python interactive shell, or REPL (Read-Eval-Print Loop), is a powerful tool for testing code snippets and exploring Python features. Simply type python in the terminal, and you’ll be greeted by the Python prompt, where you can execute Python commands and see immediate results.


Running Python code in the Ubuntu command line is an essential skill for any Python developer. Whether it’s running scripts, creating virtual environments, or exploring Python features in the interactive shell, the command line provides a powerful and flexible environment for working with Python. With the steps outlined in this article, you’ll be well-equipped to harness the full potential of Python in the Ubuntu command line.