Running Python Files in Terminal: A Simple Step-by-Step Guide for Beginners

By

If you’re just starting out with Python, one of the first things you’ll need to learn is how to run your Python scripts from the terminal. This may sound intimidating at first, but once you get the hang of it, it becomes a powerful tool in your programming toolkit. Whether you’re on Windows, macOS, or Linux, using the terminal to execute your Python files can streamline your workflow and is essential for automating tasks, testing your code, and working on projects that involve multiple scripts.

Why Use the Terminal to Run Python Files?

You might be wondering, why not just use the built-in run button in an IDE? While IDEs like VS Code and PyCharm offer great tools for Python development, running Python from the terminal provides several advantages:

  • Speed: You can quickly test small scripts without the bloat of a full IDE.
  • Control: Running scripts via the terminal gives you more control over environment variables and script arguments.
  • Remote Execution: Many servers don’t offer a graphical interface, so everything is done via the terminal.

Step 1: Install Python

Before you can run Python files in your terminal, you need to make sure Python is installed on your system. To check, open your terminal and type:

python --version

Or, depending on your system:

python3 --version

If Python is installed, you’ll see an output like Python 3.10.6. If not, you’ll need to download and install it from the official Python website.

Step 2: Write Your Python File

Next, create a simple Python file using your favorite text editor. For this example, let’s write a basic script called hello.py that prints a message:

print("Hello, world!")

Save this file to a directory you can easily navigate to from the terminal.

Step 3: Open the Terminal

Now that you’ve saved your script, it’s time to run it. Open the terminal on your system:

  • Windows: Press Windows + R, type cmd, and hit Enter.
  • macOS: Use Spotlight (Cmd + Space) and type “Terminal”.
  • Linux: Press Ctrl + Alt + T or search for “Terminal” in your apps menu.

Using the cd command, navigate to the directory where your hello.py file is saved. For example:

cd Desktop/PythonProjects

Step 4: Run the Python File

With your terminal in the correct directory, you can now run your Python file. Depending on your system, use one of the following commands:

python hello.py

or

python3 hello.py

If everything is set up correctly, the terminal should display:

Hello, world!

This simple process demonstrates how to run any Python file, no matter how complex, directly from your terminal interface.

Troubleshooting Common Issues

Here are a few common issues beginners face and how to fix them:

  • Command Not Found: If you see an error like ‘python’ is not recognized, it means Python isn’t added to your system’s PATH. During installation, you need to check “Add Python to PATH”. You can update this manually or reinstall Python.
  • Permission Denied: This might mean you don’t have permission to run a file. Try running the command with elevated privileges by using sudo (on macOS/Linux) and be cautious when doing so.
  • Wrong Python Version: If your script requires Python 3 but the terminal defaults to Python 2, you can explicitly use python3 instead.

Running Python with Arguments

Your Python scripts can also accept command-line arguments. Modify your hello.py script as follows:


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

Now run the script with an argument:

python hello.py Alice

The output will be:

Hello, Alice!

This is especially useful when creating more dynamic and interactive scripts.

Bonus Tip: Using Virtual Environments

As you progress, you’ll often work on multiple Python projects with different dependencies. This is where virtual environments shine. A virtual environment lets you install packages on a per-project basis without affecting the global Python installation.

To create one, run:

python -m venv venv

This creates a folder named venv. To activate the environment:

  • Windows: venv\Scripts\activate
  • macOS/Linux: source venv/bin/activate

You’ll know the environment is active when your terminal prompt changes to show (venv)

Enhancing Productivity

Once you’re comfortable running Python files from the terminal, you can take things even further. Here are a few suggestions:

  • Create Shell Aliases: Shorten your commonly used commands with custom aliases.
  • Use Task Automation: Combine Python with scheduling tools like cron or Task Scheduler to automate daily routines.
  • Integrate with Git: Many developers use Git along with the terminal to manage code repositories, track changes, and collaborate with others.

Recap: Quick Checklist

Let’s quickly recap the steps to run a Python file in the terminal:

  1. Install Python and ensure it’s added to your system PATH.
  2. Write a Python script and save it with a .py extension.
  3. Open the terminal and navigate to your script’s directory.
  4. Run the file using python filename.py or python3 filename.py.

Conclusion

Learning to run Python files from the terminal is a foundational skill for every aspiring programmer. It not only improves your efficiency but also prepares you for more advanced topics like scripting, automation, and working in professional development environments.

It may seem like a small step, but mastering this workflow opens the door to a much broader world of possibilities in Python development. So go ahead—open that terminal, write some Python code, and see where this powerful language can take you!