Working with the Terminal and Command Line
Running a Python program means invoking the Python interpreter and pointing it at a source file using the python command in a terminal.
From Saved Text to Running Program
Saving Python instructions in a file does not execute them. A .py file is static text stored on your computer. To make those instructions run, you use a terminal command that invokes the Python interpreter and points it at the source file.
What do you think happens?
What must happen after you save Python instructions in a file before those instructions can produce output?
Reveal answer
The file must be opened by the Python interpreter
The source file remains static text until the Python interpreter reads it, checks its syntax, and executes its instructions.
The Command Structure
The basic command for running a Python source file is python filename.py. The word python starts the Python interpreter, and filename.py identifies the source file that the interpreter should read.
Reading a Command from Left to Right
Interpret the command python hello.py hello world.
- 1Find the interpreter command
python is the command that starts the Python interpreter.
- 2Find the source filename
hello.pyis the source file that the interpreter is asked to read. - 3Find the extra values
hello and world are command-line arguments. They are additional information passed to the program.
- 4Determine whether arguments are required
Arguments are optional. Many programs can run with only the interpreter command and the source filename.
The command starts Python, runs hello.py, and passes hello and world as command-line arguments.
What Happens After Enter
Pressing Enter begins a sequence of events. The operating system launches the Python interpreter. The interpreter opens and reads the named source file, parses the code to check whether its syntax is valid, and then executes the statements in order from top to bottom. If the program prints output, that output appears in the terminal. If the program encounters an error, the interpreter stops and displays an error message.
Moving Arguments into the Program
Anything placed after the source filename can be a command-line argument. In python hello.py hello world, hello and world are extra pieces of information passed to the program when it starts. The Python code can read and use those arguments to change its behavior, allowing the same source file to be run with different inputs without editing the file each time.
| Command part | Role |
|---|---|
| python | Starts the Python interpreter |
| hello.py | Names the source file to run |
| hello world | Provides optional command-line arguments |
The three roles in the source example python hello.py hello world.
Running the Same File Again
Running a source file does not consume or replace the file. The interpreter reads the static text and creates an active process to execute it. After that process ends, the same source file remains available for another run. Supplying different command-line arguments lets the same program receive different information without changing the source file.
When troubleshooting a terminal run, separate the three questions: Did the command start the interpreter? Did the interpreter find and validate the source file? What happened while the program executed? This distinction helps you interpret whether the result is program output or an error message.
Mistakes with Terminal Commands
Common mistake Expecting a saved .py file to execute automatically
What it looks likeThe source file has been saved, but no terminal command has been entered.
Why it failsA source file is static text on disk. It does not become a running process until the Python interpreter reads and executes it.
Do this insteadUse the python command followed by the source filename.
Common mistake Treating the filename as the interpreter command
What it looks likeConfusing hello.py with python in python hello.py.
Why it failspython starts the interpreter, while
hello.pyidentifies the source file to be read.Do this insteadRead the command from left to right: interpreter command first, source filename second.
Common mistake Assuming every value after the filename is part of the filename
What it looks likeReading hello and world in python hello.py hello world as part of the file name.
Why it failsValues after the filename are command-line arguments passed to the program.
Do this insteadSeparate the source filename from any optional arguments that follow it.
Common mistake Assuming a process changes the source file
What it looks likeThinking that running hello.py removes or permanently transforms the file.
Why it failsThe source file remains static on disk after the active process ends.
Do this insteadThink of execution as the interpreter reading the file and creating a temporary running process.
Practice the Execution Trace
Trace the command python report.py morning team. Identify the interpreter command, the source filename, and the command-line arguments. Then describe what happens after Enter is pressed, from interpreter launch through execution and terminal output or an error message.
Hints
- The interpreter command appears first.
- The source filename ends in .py.
- Values after the filename are optional command-line arguments.
- The interpreter reads and validates the source file before executing its statements.
- 1Locate python: it starts the Python interpreter.
- 2Locate
report.py: it is the source file to be read. - 3Locate morning and team: they are command-line arguments.
- 4Trace the run: the operating system launches the interpreter, the interpreter reads and validates the file, and the code executes in order.
- 5Check the result: program output appears in the terminal, or an error message appears if execution encounters an error.
Key Takeaways
- Running a Python program from a terminal means invoking the Python interpreter and giving it a .py source filename.
- The interpreter reads the source file, checks its syntax, and executes its statements in order.
- The source file is static text on disk, while execution creates an active process that eventually ends.
- Values typed after the filename are optional command-line arguments that the program can read and use.
- Program output or an error message appears in the terminal after the command runs.