Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run a Python file in terminal

Getting Started

Welcome to the world of programming! Today, we'll focus on running a Python file in the terminal. The terminal, also known as a command line interface, is where you type commands to control your computer. Think of it as a text-based alternative to your computer's graphical interface. Now, let's dive in!

Understanding Python Files

Python files are simple text files that contain Python code. They typically end with a .py extension. For example, you might have a Python file called that looks something like this:

print("Hello, world!")

This file contains a single Python command: print("Hello, world!"). The print() function prints the string within its parentheses to the terminal.

Installing Python

Before we can run Python files, we need to make sure Python is installed on your computer. Python can be downloaded from its official website (

Creating Your First Python File

Let's create your first Python file. Open a text editor (like Notepad or TextEdit), type the following line of code, and save it as

print("Hello, World!")

This command will display the text "Hello, World!" when executed.

Now, we need to navigate to the location of this file in the terminal. The cd (Change Directory) command is used for this purpose. If your file is in the Documents folder, you would navigate to it like this:

cd Documents

Think about cd like the GPS of your terminal. It helps you move from one folder (or directory) to another.

Running Your Python File

Finally, we're ready to run the file! In the terminal, type the following command:


This command tells Python to execute (or run) the file named If everything is set up correctly, you should see Hello, World! displayed in your terminal.

Interpreting Errors

You might encounter errors while running your Python file. Errors are like your programming GPS telling you've taken a wrong turn. They provide valuable information about what went wrong, so you can fix your code. For example, if you mistype print as prnt, Python will give you a NameError telling you that prnt is not defined.

Running More Complex Python Files

Python files can contain more than one line of code, and they can also import other Python files using the import command. Let's create a new Python file named

def greet(name):
    print(f"Hello, {name}!")


In this file, we define a function greet(name) that prints a personalized greeting. We then call this function with the argument "Alice". If you navigate to this file in your terminal and run it using python, you should see Hello, Alice! displayed in the terminal.

Automating Tasks with Python

Python is great for automating tasks. For instance, you could write a Python script to rename all the files in a directory, or to download all the images on a webpage. The possibilities are endless!


And there you have it! You've learned how to run a Python file in the terminal. Along the way, you've also learned about Python files, the print() function, the cd command, and how to interpret errors.

Remember, the terminal is a powerful tool for interacting with your computer. It might seem intimidating at first, but with practice, you'll become more comfortable with it. Just like learning a new language, it's all about immersion.

In programming, there's always more to learn, and Python is no exception. Keep experimenting, keep making mistakes, and keep learning. You're now one step closer to becoming a Python wizard!

May the codes be with you!