Running Python from the command line

Python is most useful when it can be run deliberately and repeatedly as part of a real workflow. That usually means running programs from a terminal or command prompt, where we can control inputs, see output clearly, and integrate Python into larger systems. This lesson orients us to how Python is run from the command line and what happens when it starts up.

Verifying that Python is installed

Before we can run any Python code, the Python interpreter must be available on the system. From a terminal or command prompt, we can ask Python to report its version.

python --version

If Python is installed and on the system path, this command prints a version number and exits. That tells us the interpreter is available and ready to run programs.

Running Python interactively and running a script

Python can be run in two different modes. One is interactive mode, where we start the interpreter and type code directly. The other is script mode, where Python executes code stored in a file.

To start Python interactively, we run the interpreter without giving it a file.

python

Python starts, prints a short startup message, and waits for input. In contrast, when we run a script, Python reads code from a file, executes it, and then exits.

Executing a Python file from the command line

To run a Python program stored in a file, we pass the file name to the interpreter.

python generate_page.py

Python loads the file, executes its contents from top to bottom, and then terminates. This is how most real Python programs are run, whether they generate files, process data, or act as part of a larger system.

Understanding the current working directory

When Python runs a script, it does so relative to the current working directory. This is the directory the terminal is “in” when the command is executed.

If a Python file is not in the current directory, Python will not find it unless a path is provided. Likewise, any files the program reads or writes are resolved relative to this directory unless absolute paths are used.

Interpreting startup and runtime messages

When Python starts, it may print brief startup information in interactive mode. When running a script, it is usually silent unless the program produces output.

If something prevents the program from running, Python reports the issue directly in the terminal. These messages are part of normal execution and provide immediate feedback about how the interpreter is processing the program.

Conclusion

We now know how to start Python, how to run it interactively or with a script file, and how the command line environment affects execution. That is enough orientation to run real Python programs intentionally and to understand what the interpreter is doing when it starts and stops.