Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run Python file in terminal

Introduction

Python is an easy-to-learn and versatile programming language that has gained immense popularity in recent years. Running Python programs is an essential skill every budding programmer needs to master. In this tutorial, we will walk you through the process of executing Python files in the terminal.

If you are new to programming, this tutorial will help you understand the basics of running Python files, as well as some common terms and concepts. We will provide code examples and analogies to make the learning process more intuitive. So, let's get started!

Prerequisites

Before we begin, make sure you have Python installed on your system. You can check if Python is installed and its version by opening a terminal (Command Prompt on Windows or Terminal on macOS/Linux) and typing the following command:

python --version

If Python is installed, the terminal will display the version number. If it's not installed, you can download and install Python from the official website.

Creating a Python File

To run a Python file, first, we need to create one. A Python file is a text file containing Python code and has a .py extension. You can create a Python file using any text editor like Notepad, TextEdit, or code editors like Visual Studio Code, Sublime Text, Atom, etc.

Let's create a simple Python file named hello_world.py with the following code:

print("Hello, World!")

This program simply prints "Hello, World!" to the terminal when executed. Save the file in a folder on your computer, and make a note of the folder location. We will use this location to navigate to the file in the terminal.

Opening the Terminal

To run our Python file, we need to use a terminal (also called the command-line interface or CLI). The terminal is a text-based interface to interact with your computer's operating system. You can perform various tasks, like running programs and managing files, by entering commands in the terminal.

To open the terminal on your computer, follow the steps below:

  • Windows: Press Win + R to open the Run dialog, type cmd, and press Enter. This will open the Command Prompt.
  • macOS: Press Cmd + Space to open Spotlight Search, type terminal, and press Enter.
  • Linux: Press Ctrl + Alt + T or search for "terminal" in the application menu.

Once the terminal is open, we need to navigate to the folder containing the Python file using the cd (Change Directory) command. The cd command allows you to move between directories in the terminal. To navigate to the folder containing hello_world.py, type the following command in the terminal and press Enter:

cd /path/to/your/folder

Replace /path/to/your/folder with the actual path to the folder where you saved hello_world.py. For example, if you saved the file in a folder named python_projects on your desktop, the command would look like this:

  • Windows: cd C:\Users\YourUsername\Desktop\python_projects
  • macOS/Linux: cd /Users/YourUsername/Desktop/python_projects

Replace YourUsername with your actual username on your computer.

Running the Python File

Now that we've navigated to the folder containing the Python file, we can run it using the python command followed by the name of the file. Type the following command in the terminal and press Enter:

python hello_world.py

The terminal will execute the Python file and display the output, which in this case is "Hello, World!".

Congratulations! You've just successfully run a Python file in the terminal.

Running Python Code with Command-Line Arguments

Python programs can also accept command-line arguments, which are additional inputs provided by the user when running the program. These arguments can be used to customize the program's behavior or provide input data.

Let's create a new Python file named greet.py with the following code:

import sys

name = sys.argv[1]
print(f"Hello, {name}!")

In this program, we import the sys module, which is a built-in Python module containing system-specific functionality. We use the sys.argv list to access command-line arguments. sys.argv is a list that contains the name of the script as its first element and any command-line arguments as its subsequent elements.

In our greet.py example, we expect the user to provide a name as a command-line argument. We then access the second element of the sys.argv list (index 1) to get the name and use it to print a personalized greeting.

To run greet.py with a command-line argument, navigate to the folder containing the file using the cd command, as explained earlier. Then, type the following command in the terminal and press Enter:

python greet.py YourName

Replace YourName with your actual name or any other text. The terminal will execute the Python file and display a personalized greeting, like "Hello, John!".

Understanding Errors

When running Python files, you may encounter errors that prevent your program from executing successfully. These errors can be caused by various issues, like syntax errors, incorrect file paths, or missing modules.

When an error occurs, Python will display an error message in the terminal, providing information about the type of error and the location in the code where the error occurred. Understanding these error messages is essential for debugging and fixing your code.

For example, if you accidentally mistype the print function in the hello_world.py file as prit and try to run the file, you will see the following error message:

File "hello_world.py", line 1
  prit("Hello, World!")
                      ^
SyntaxError: invalid syntax

The error message tells you that there is a SyntaxError in the code, and it points to the exact location of the error (line 1). To fix the error, you need to correct the typo in the print function and run the file again.

Conclusion

In this tutorial, we've learned how to run Python files in the terminal by navigating to the folder containing the file and using the python command followed by the file name. We've also discussed how to provide command-line arguments to customize the program's behavior and how to understand and fix errors.

Running Python files in the terminal is an essential skill for every programmer, as it helps you understand the basics of program execution and provides a foundation for working with more complex tools and environments. As you continue learning programming, you'll encounter more advanced concepts and techniques, but the ability to run Python files in the terminal will always be a valuable skill in your toolbox.