Concepts / Working with the Terminal and Command Line

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.

  • Programming

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?

  • The file must be opened by the Python interpreter
  • The file automatically becomes a running process
  • The filename must be changed
Reveal answer

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.

file is readinstructions executeoutput appearshello.pystatic textPython interpreterreads and validatesRunning processactive executionTerminal outputprogram result
How does the Python interpreter receive and execute instructions from a .py source file?

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.

thenoptionally followed byandpythonstarts interpreterhello.pysource filehellooptional argumentworldoptional argument
What is the difference between the interpreter command, the source filename, and optional command-line arguments?

Reading a Command from Left to Right

Interpret the command python hello.py hello world.

Find the interpreter command: python is the command that starts the Python interpreter.

Find the source filename: hello.py is the source file that the interpreter is asked to read.

Find the extra values: hello and world are command-line arguments. They are additional information passed to the program.

Determine 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.

Enterlaunchesvalid syntaxproducesTerminalcommand enteredOperating systemlaunches interpreterPython interpreterreads and parsesCode executiontop to bottomTerminal resultoutput or error
What happens next when a user types a Python command with a source filename in the terminal?

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.

follow filenamepasses valuesuses valueshello worldtyped after filenamepython hello.pystarts programRunning programreceives argumentsProgram behaviorcan use input
How do values typed after the .py filename move from the terminal into the running Python program?
Command partRole
pythonStarts the Python interpreter
hello.pyNames the source file to run
hello worldProvides optional command-line arguments

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.

before runningwhile runninghello.pystatic texthello.pystatic textNo active processRunning processactive execution
What changes when a source file is run, and what remains available afterward?

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

  • Expecting a saved .py file to execute automatically

    A source file is static text on disk. It does not become a running process until the Python interpreter reads and executes it.

    Fix: Use the python command followed by the source filename.

  • Treating the filename as the interpreter command

    python starts the interpreter, while hello.py identifies the source file to be read.

    Fix: Read the command from left to right: interpreter command first, source filename second.

  • Assuming every value after the filename is part of the filename

    Values after the filename are command-line arguments passed to the program.

    Fix: Separate the source filename from any optional arguments that follow it.

  • Assuming a process changes the source file

    The source file remains static on disk after the active process ends.

    Fix: Think of execution as the interpreter reading the file and creating a temporary running process.

Practice the Execution Trace

EASY

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.
  1. Locate python: it starts the Python interpreter.
  2. Locate report.py: it is the source file to be read.
  3. Locate morning and team: they are command-line arguments.
  4. Trace the run: the operating system launches the interpreter, the interpreter reads and validates the file, and the code executes in order.
  5. Check 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.