SECTION 1
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.
1991 • Python 0.9 Released
2000 • Python 2.0 Arrives
python command becomes standard on Linux and macOS systems.2008 • Python 3.0 Released
python3 to distinguish it from older Python 2 installations.2020s • Python Dominates Education
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.
SECTION 2
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.KEY TAKEAWAY
python file.py is you saying, "Hey chef, please follow this recipe!"SECTION 3
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.
SECTION 4
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:
hello.py
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.
Common Gotcha: "python is not recognized"
python3 instead.SECTION 5
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 |
Cross-platform command comparison
SECTION 6
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.
Create and Run temp_converter.py
- 1
Step 1 — Write the Script
Open your text editor and type the following code. Save the file astemp_converter.pyon your Desktop.fahrenheit = 98.6 celsius = (fahrenheit - 32) * 5 / 9 print(f"{fahrenheit}°F is {celsius:.2f}°C")File saved astemp_converter.py - 2
Step 2 — Open the Terminal
Open your terminal application. On Windows, press the Windows key and type "cmd". On macOS, press Cmd + Space and type "Terminal".Terminal window is now open - 3
Step 3 — Navigate to the Desktop
Typecd Desktopand press Enter. Verify the file is there by listing the directory contents withls(macOS/Linux) ordir(Windows). You should seetemp_converter.pyin the output.Working directory is now Desktop - 4
Step 4 — Run the Script
Typepython temp_converter.py(orpython3 temp_converter.pyon macOS/Linux) and press Enter.98.6°F is 37.00°C - 5
Step 5 — Modify and Re-run
Go back to your text editor and change the value offahrenheitto212. 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.212°F is 100.00°C
Pro Tip: The Up Arrow Key
SECTION 7
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 |
Comparison of Python execution methods
KEY TAKEAWAY
python file.py gives you a skill that transfers to every operating system and every professional environment.SECTION 8
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 |
From beginner to advanced command-line usage
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.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
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.Show answer
hello.py to it. The interpreter opens the file, reads the code line by line from top to bottom, translates each line, and executes it. If the code includes a print() statement, the output is sent back to the terminal for you to see.Question 2: BASIC CALCULATION
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?Show answer
cd Documents and press Enter to navigate to the Documents folder. Then you would type python area.py and press Enter. The output will be Area: 60 because the script multiplies 12 × 5 to get 60, then prints it with the label "Area:". If you wanted to verify the file was in the right place first, you could also type dir to list the files.Question 3: INTERMEDIATE
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.Show answer
cd to navigate to the folder where greet.py is actually saved. They can verify their current location with pwd or cd (alone on Windows). Second, the file name might be misspelled — perhaps it was saved as Greet.py (with a capital G) or greet.txt instead. They should use ls or dir to see the actual file names. Third, the file might not have been saved at all — the student should go back to their editor and confirm the file was saved with the correct .py extension.Question 4: APPLIED
Show answer
earned = 85
total = 100
percentage = (earned / total) * 100
print(f"Your grade: {percentage}%")
Save this as grade.py in a convenient location like your Desktop. Open your terminal, type cd Desktop to navigate there, then run python grade.py (or python3 grade.py on macOS/Linux). The output will be "Your grade: 85.0%". You could also modify the earned and total variables to calculate different grades and re-run the script.Question 5: CRITICAL THINKING
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?Show answer
python main.py from the new folder, Python will raise a ModuleNotFoundError because it cannot find helper.py. This happens because when Python encounters an import statement, it searches for the module in several locations, starting with the directory where the script being executed lives. Since helper.py is no longer in the same folder as main.py, Python cannot locate it. This reveals that the interpreter uses the script's location (and the working directory) as the starting point for finding other files. To fix this, you would either move both files to the same folder or configure Python's module search path — a concept you will explore when studying packages and modules.SUMMARY
Lesson Summary
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.