Debugging Python Scripts
A Python script is a text file containing Python code, saved with the .py extension, that the interpreter executes from top to bottom.
Two Ways to Run Python
When learning Python, you can enter an instruction directly into the interactive interpreter or place a collection of instructions in a saved script. The interpreter is useful when you want immediate feedback from a small experiment. A script is more suitable when you are building a complete program that must be organized, saved, reused, or run again.
What do you think happens?
You need to repeat a multi-step data analysis tomorrow. Which approach is more suitable: entering each instruction again in the interactive interpreter or saving the instructions in a script?
Reveal answer
Answer: Save the instructions in a Python script
A script is persistent and reusable. The interactive interpreter is designed for one-off commands and experimentation, and its entered code disappears when the interpreter is closed.
The Interactive Interpreter
The interactive Python interpreter acts as a sandbox for experimentation. You type a command, press Enter, and immediately see the result. This makes it useful for learning, testing a small piece of code, checking how a function behaves, or exploring Python's built-in features. Each command is evaluated immediately, and the interpreter presents a prompt for the next command.
| Interactive interpreter | Saved script |
|---|---|
| Commands are entered one at a time | Python code is stored together in a text file |
| Results are shown immediately | The interpreter runs the saved code as a program |
| Useful for experimentation and small tests | Useful for complete, reusable programs |
| Entered code disappears when the interpreter closes | The file can be saved and run again |
| A prompt waits for the next command | There is no interactive prompt during execution |
Anatomy of a Python Script
A Python script is a text file containing Python code, saved with the .py extension, that the interpreter executes from top to bottom.
The script is saved as plain text. You can create it with a simple text editor, a code-focused editor, or an integrated development environment. The important requirement is that the editor saves plain text rather than a formatted document. The .py extension identifies the file as containing Python code for the interpreter to execute.
Execution from Beginning to End
When you run a script, the interpreter opens the file and reads the code from the beginning to the end. It then executes each instruction in order, as if you had entered the instructions one by one in the interactive interpreter. In script mode, there is no prompt and no interactive feedback during execution. The script runs silently unless you explicitly use print() to display output.
Tracing a Script with an Error
A saved script contains three ordered instructions. The interpreter completes the first instruction, encounters an error during the second instruction, and has a third instruction after it. What happens?
Start at the beginning: The interpreter opens the script and begins reading its contents from the start.
Execute the first instruction: The first instruction is executed in its position in the script.
Reach the error: When the interpreter encounters an error, it stops and reports the problem.
Do not continue: The instruction after the error is not reached because execution has already stopped.
A script normally proceeds from top to bottom, but an error interrupts execution at the point where the problem occurs.
Why Complex Work Needs Scripts
Consider a program that analyzes data from a hundred files, performs calculations, and generates a report. Entering every step into the interactive interpreter would be tedious, error-prone, and difficult to reproduce. If you made a mistake or wanted to run the program again later, you would need to retype the instructions. A script keeps the instructions available so the complete process can be run again.
Scripts become increasingly important as a program grows. They provide a persistent place to keep related instructions, allow code to be organized logically, and can include comments explaining what sections do. These qualities make a script more reliable to reuse than a sequence of commands entered from memory.
- Use the interactive interpreter for learning, quick experiments, small tests, and exploring how a function or module works.
- Use a script when solving a real problem or building a complete program.
- Use a script when you want to save code and run it again later.
- Use a script when related instructions need logical organization and comments.
Common Execution Mistakes
Treating the interactive interpreter as permanent storage
The code entered in the interactive interpreter disappears when the interpreter is closed.
Fix:
Move code that must be reused into a plain-text Python script saved with the .py extension.Expecting a running script to behave like the interactive interpreter
Script mode has no interactive prompt and runs silently unless output is explicitly displayed with print().
Fix:
Use the interactive interpreter for immediate feedback, or explicitly use print() in a script when output should be displayed.Assuming an error allows later instructions to continue
The interpreter stops and reports the problem when an error occurs.
Fix:
Treat the error location as the point where execution stopped, then inspect the preceding and current instructions.Saving Python code as a formatted document
The source requires a plain-text file rather than a formatted document.
Fix:
Use a text editor, code-focused editor, or integrated development environment that saves plain text, and use the .py extension.Retyping a growing program one command at a time
This approach is tedious, error-prone, and difficult to reproduce reliably.
Fix:
Organize the instructions in a reusable script.
Practice the Workflow
Describe the complete workflow for turning Python instructions into a reusable program. Include the type of editor or file format required, the file extension, what the interpreter does when the file runs, and what happens if an error occurs.
Hints
- Begin with writing Python code in an editor.
- Mention that the file must be plain text and use the .py extension.
- Explain that the interpreter reads and executes the script from top to bottom.
- State that an error stops execution and the interpreter reports the problem.
You want to explore how one Python function behaves before adding it to a larger program. Decide whether the interactive interpreter or a saved script is the better starting point, and explain why.
Hints
- Look for the option that gives immediate feedback.
- The source recommends one environment specifically for experimentation and testing a small piece of code.
Key Takeaways
- A Python script is a plain-text file containing Python code and normally uses the .py extension.
- The interactive interpreter evaluates commands immediately and is best suited to experimentation and small tests.
- A script is read and executed from top to bottom, without an interactive prompt during execution.
- An error stops script execution and causes the interpreter to report the problem.
- Scripts are persistent, reusable, and easier to organize for complex or repeatable work.
Key Takeaways
- Use the interactive interpreter for immediate feedback during experimentation.
- Use a saved .py script for complete programs and work that must be reused.
- Create a script in a plain-text editor, save it with the .py extension, and run it with the interpreter.
- The interpreter executes a script from top to bottom and stops when an error occurs.
- Scripts make complex tasks manageable by preserving, organizing, and reusing related instructions.