Historical Context & Motivation
Before graphical user interfaces became widespread, programmers interacted with computers almost exclusively through a command line — a text-based interface where you type instructions and the computer responds. When Guido van Rossum created Python in the late 1980s, he designed it to be run from this command line environment. Even today, with all the fancy code editors and integrated development environments (IDEs) available, running a Python script from the command line remains the most fundamental and universal way to execute your code.
Understanding how to run scripts from the command line gives you direct control over your programs. It is also the foundation for automation, server-side programming, and professional software development workflows. Whether you are building a simple calculator or training a machine learning model, the journey starts with a single command: python file.py.
python command becomes standard on Linux and macOS systems.python3 to distinguish it from older Python 2 installations.So how does a plain text file on your computer become a running program? That is exactly the question this lesson answers. You will learn to create a Python file, open a terminal, navigate to the right folder, and execute your script — all with a single, powerful command.
Core Principles & Definitions
Before you can run a Python script, you need to understand a few foundational concepts. These building blocks will help you troubleshoot problems and feel confident every time you open a terminal.
Python Script
.py extension containing Python code. Your computer does not execute this file directly — it passes it to the Python interpreter.Python Interpreter
.py file, translates each line into instructions the computer understands, and executes them one by one.Terminal / Command Prompt
Working Directory
python file.py, the terminal looks for that file in the working directory.PATH Environment Variable
python. If Python is not on your PATH, the terminal will not recognize the command.python file.py is you saying, "Hey chef, please follow this recipe!"Visual Explanation — From File to Output
The diagram below shows the complete journey of running a Python script. It starts with you typing a command in the terminal, moves through the Python interpreter, and ends with output displayed on your screen. Follow the arrows to trace each step.
.py file. Step 4 is where your code actually runs, and Step 5 displays the result.Notice that the interpreter processes your code line by line from top to bottom. This is why the order of statements in your script matters. If you reference a variable before defining it, Python will raise an error because it has not encountered that line yet.
How It Works — Step by Step
Step 1: Create Your Python File
Open any text editor — Notepad on Windows, TextEdit on macOS (in plain text mode), or a code editor like VS Code. Type your Python code and save the file with a .py extension. For example, create a file called hello.py with this content:
print("Hello, World!")Step 2: Open Your Terminal
On Windows, press the Windows key, type "cmd" or "PowerShell", and press Enter. On macOS, open Spotlight (Cmd + Space), type "Terminal", and press Enter. On Linux, look for Terminal in your applications menu or press Ctrl + Alt + T.
Step 3: Navigate to Your File's Location
Use the cd (change directory) command to navigate to the folder where you saved your file. For example, if you saved hello.py on your Desktop, you would type cd Desktop and press Enter. You can verify you are in the right place by typing dir (Windows) or ls (macOS/Linux) to list the files in your current directory.
Step 4: Run the Script
Type python hello.py and press Enter. If you are on macOS or Linux and your system has both Python 2 and Python 3 installed, you may need to type python3 hello.py instead. The interpreter will read your file and execute the print() function, displaying Hello, World! in the terminal.
python3 instead.Essential Terminal Commands for Python
To run Python scripts effectively, you need to be comfortable with a handful of terminal commands. The diagram below maps out the most important ones and shows how they relate to each other in a typical workflow.
| Command | Windows | macOS / Linux |
|---|---|---|
| List files | dir | ls |
| Change directory | cd folder_name | cd folder_name |
| Show current path | cd (alone) | pwd |
| Run Python script | python file.py | python3 file.py |
| Check Python version | python --version | python3 --version |
Worked Example — Building and Running a Temperature Converter
Let's walk through a complete example from scratch. We will create a Python script that converts a temperature from Fahrenheit to Celsius, then run it from the command line.
temp_converter.py on your Desktop.
fahrenheit = 98.6
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit}°F is {celsius:.2f}°C")temp_converter.pycd Desktop and press Enter. Verify the file is there by listing the directory contents with ls (macOS/Linux) or dir (Windows). You should see temp_converter.py in the output.python temp_converter.py (or python3 temp_converter.py on macOS/Linux) and press Enter.fahrenheit to 212. Save the file, then return to your terminal and press the Up arrow key to recall the previous command. Press Enter to run the updated script.Command Line vs. Other Ways to Run Python
Running scripts from the command line is not the only option for executing Python code. You might have used an online editor, a Jupyter notebook, or an IDE's built-in run button. Each method has its place, but understanding their trade-offs helps you choose the right tool for the job.
| Method | Strengths | Limitations |
|---|---|---|
| Command Line (python file.py) | Works everywhere, fast, no extra software needed, great for automation and scripting | No built-in code highlighting, no auto-complete, must navigate with cd commands |
| IDE Run Button (VS Code, PyCharm) | Syntax highlighting, auto-complete, integrated debugging, click-to-run convenience | Heavier software, hides what is actually happening behind the scenes |
| Interactive REPL (typing python alone) | Instant feedback, great for testing small snippets and exploring | Code is not saved, impractical for multi-line programs |
| Online Editors (Replit, Google Colab) | No installation required, shareable, accessible from any device | Requires internet, limited access to local files and system features |
python file.py gives you a skill that transfers to every operating system and every professional environment.Connection to Advanced Workflows
Running a single script from the command line is your starting point, but professional developers build on this foundation every day. As you grow as a programmer, you will encounter more powerful ways to use the command line with Python. Here is a preview of what lies ahead.
| What You Know Now | What Comes Next |
|---|---|
python file.py | python file.py arg1 arg2 — passing command-line arguments to your script |
| Running one script at a time | Using virtual environments to manage different projects and their dependencies |
| Seeing output in the terminal | Redirecting output to files: python file.py > output.txt |
| Manually running scripts | Automating scripts with cron jobs (Linux/macOS) or Task Scheduler (Windows) |
| Single-file programs | Multi-file projects with modules and packages using import |
Each of these advanced skills builds directly on the same python file.py command you are learning today. By mastering the fundamentals now, you are setting yourself up for a smooth transition into more complex development workflows. Think of today's lesson as learning to drive in a parking lot before heading out onto the highway.
Practice Problems
python hello.py in the terminal, what role does the Python interpreter play? Explain in your own words what happens between pressing Enter and seeing output.area.py with this content:
length = 12
width = 5
area = length * width
print("Area:", area)
You save it in your Documents folder. Write the exact terminal commands (for Windows) to navigate to Documents and run this script. What output will you see?python: can't open file 'greet.py': [Errno 2] No such file or directory
List three possible causes for this error and describe how to fix each one.helper.py contains a function, and main.py imports and uses that function. If you move main.py to a different folder but leave helper.py behind, what will happen when you try to run python main.py from the new location? Why does this happen, and what does it reveal about how the Python interpreter finds files?Lesson Summary
Running a Python script from the command line is the most fundamental way to execute your code. You create a Python script (a text file with a .py extension), open your terminal or command prompt, use cd to navigate to the folder containing your file, and type python file.py to execute it. The Python interpreter reads your code line by line, translates it into machine instructions, and produces output in the terminal.
Key details to remember: use python3 on macOS/Linux if python does not work; check that Python is on your PATH if the command is not recognized; and use ls or dir to verify you are in the correct working directory. This skill is the foundation for everything from automation to professional software development, and mastering it now will serve you throughout your programming journey.