How To Run A Python Program In CLI

In this tutorial, we will learn how to run a Python program using the Command Line Interface (CLI).

The Python CLI is an incredibly useful and versatile tool for developers and can help make your development process smoother and more productive.

Whether you’re just starting out with Python or have been using it for years, you’ll find the advice in this tutorial helpful.

Step 1: Install Python

First, make sure that you have Python installed on your system. If you don’t already have it, you can download the latest version of Python from the official Python website. Follow the installation instructions provided on the website for your operating system.

Step 2: Open the Command Prompt or Terminal

To run Python from the CLI, you first need to open your system’s terminal or command prompt. This process can vary depending on your operating system. Here are the instructions for some popular operating systems:

  • Windows: Press the Windows Key + R to open the Run dialog box. Then, type “cmd” and press Enter.
  • macOS: Press Cmd + Space to open Spotlight Search, type “Terminal,” and press Enter.
  • Linux: Press Ctrl + Alt + T to open a terminal window.

Step 3: Create a Python Script

Now that we have our command prompt or terminal open, let’s create a simple Python script. Use your favorite text editor to create a new file named with the following content:

Save the file in a location that’s easy to access from the command prompt or terminal.

Step 4: Navigate to the Script Directory

In the command prompt or terminal, navigate to the directory where you saved the file. You can use the following commands to help you navigate:

  • cd [Directory]: Changes the current directory to the specified directory.
  • ls (macOS/Linux) or dir (Windows): Lists the contents of the current directory.
  • cd ..: Moves up one directory level.

Once you’ve navigated to the correct directory, all that’s left to do is run the script!

Step 5: Run the Python Script

To execute your Python script, simply enter the following command in the terminal or command prompt:

You should see the output of the script, which in our case is a simple “Hello, World!” message. Congratulations! You’ve successfully run a Python script from the CLI.

Hello, World!

Full Code


In this tutorial, we learned how to run a Python program using the command line interface (CLI).

We covered the steps needed to install Python, open the command prompt or terminal, create a Python script, navigate to the script directory, and run the script from the CLI.

With this knowledge, you are better equipped to streamline your Python development process and quickly test and execute your scripts directly from the command line.