Running Python in Interactive Mode
The standard procedure for writing Python programs consists of four steps: open an editor, type code, save as a .py file, and run using the python command.
From Editor to Output
Running a Python program is not one isolated action. The program passes through a sequence of states: you open an editor, type code, save that code as a file, and run the file with the python command. Understanding this sequence makes the workflow easier to follow and makes errors easier to diagnose.
The Four-Step Procedure
- 1Open an editor where you can write the program.
- 2Type the Python code into the editor.
- 3Save the program as a file with the .py extension.
- 4Run the saved file using the python command.
The first two steps create the program in the editor. At this point, the code exists in the editor's memory. The third step writes the code to disk as a persistent file. The fourth step tells the Python interpreter to read that saved file, execute its instructions, and produce output.
The .py file extension is the standard convention for Python scripts. It identifies the file as containing Python code.
A Greeting Program Walkthrough
Greeting Someone by Name
Trace the workflow for a program that greets someone by name.
- 1Open and type
Open an editor and type the code for the greeting program. The code exists in the editor's memory while you work on it.
- 2Save
Save the code as a file whose name ends with .py. The program now exists as a persistent file on disk.
- 3Run
Use the python command to run the saved file. The interpreter reads the file and executes its instructions.
- 4Observe output
The executing program produces the greeting as output.
The complete workflow moves the greeting from editor memory to a saved .py file, then to execution and produced output.
This example shows why the save step belongs between editing and running. If the greeting is changed in the editor but the changed version is not saved, running the file uses the version that is already on disk rather than the unsaved editor content.
What do you think happens?
You change the greeting in the editor and immediately run the file without saving. Which version does the interpreter read?
Reveal answer
The saved version of the file on disk
The python command causes the interpreter to read the file that was saved to disk. Changes that remain only in editor memory have not yet become part of that file.
Names Commands and Locations
Successful execution depends on several details matching. The saved file needs the .py extension, the command needs to be typed correctly, and the command needs to be run from the directory containing the saved file. A mismatch at any of these points can prevent the intended program from running.
Workflow Mistakes
Common mistake Forgetting to save before running
What it looks likeThe editor contains recent changes, but the saved file still contains an earlier version.
Why it failsThe python command reads the saved file on disk, not changes that remain only in editor memory.
Do this insteadSave the file before using the python command.
Common mistake Using the wrong file extension
What it looks likeThe program is saved without the .py extension.
Why it failsThe .py extension is the standard convention that identifies a Python script.
Do this insteadSave the program with a filename ending in .py.
Common mistake Running from the wrong directory
What it looks likeThe saved file is in one directory, but the command is run from another directory.
Why it failsThe command must be able to identify the saved file being run.
Do this insteadRun the command from the directory containing the saved file.
Common mistake Mistyping the python command
What it looks likeThe command or the filename does not match the intended program.
Why it failsThe interpreter command and the saved file must be specified correctly.
Do this insteadCheck the command spelling and confirm that it identifies the saved .py file.
A Reliable Pre-Run Check
- 1Confirm that the intended code is present in the editor.
- 2Save the program before attempting to run it.
- 3Confirm that the saved filename ends in .py.
- 4Check that the command uses the correct python command.
- 5Make sure the command is run from the directory containing the saved file.
- 6If the expected result does not appear, check each workflow state in order.
A program is visible in the editor, but running it does not use the latest changes. Identify the missing workflow transition and explain the correction.
Hints
- Compare the code in the editor with the code saved on disk.
- Check whether the latest changes were saved before the python command was used.
Workflow Summary
- 1The standard procedure is to open an editor, type code, save a .py file, and run it with the python command.
- 2Code first exists in editor memory, where unsaved changes can be lost if the computer crashes.
- 3Saving writes the code to disk as a persistent file.
- 4Running the python command causes the interpreter to read the saved file, execute its instructions, and produce output.
- 5When execution fails or uses old code, check saving, the .py extension, the directory, the filename, and the command syntax.
At a glance
- Create and run a Python program by opening an editor, typing code, saving a .py file, and using the python command.
- Recognize the transition from temporary editor memory to a persistent file on disk.
- Remember that the interpreter reads and executes the saved file.
- Troubleshoot by checking the save status, file extension, directory, filename, and command spelling.