Run Python and read errors
Let's put the function on your own screen. You read it and predicted its result in the first lesson. There are two things to keep separate now: the instructions inside the Python file, and the command telling your computer to run that file.
I will use Windows PowerShell for the exact commands. If you use macOS or Linux, the small comparison below gives the corresponding commands. The Python file itself stays the same.
We will make the first run inspectable by learning to:
- Find the working folder and the saved file.
- Select the Python interpreter you intended.
- Separate a command failure from an error inside Python.
- Use an error message to investigate one small defect.
Being able to reproduce a run yourself lets you check the AI's claim that its change works. Without the file, interpreter and working folder, even a correct function can appear broken for the wrong reason.
Know which program is listening
A terminal is a window where you can send text commands to a running program. The program reading ordinary commands here is a shell. PowerShell is one shell; Python is another program the shell can start.
On Windows, open Terminal or PowerShell from the Start menu. You may see something like:
PS C:\Users\Amina>
That is the prompt. It tells you the shell is waiting and shows a folder. Do not type the prompt itself when you copy a command.
Run:
Get-Location
The result names the working folder, the folder from which the shell resolves relative paths. A path is an address for a file or folder. An absolute path includes its starting location; a relative path starts from the current working folder.
List the files in that folder:
Get-ChildItem
Then use Set-Location to move into a folder you chose for your own book work. Quotes keep a path containing spaces together:
Set-Location "C:\Users\Amina\Documents\Book Work\book-reader"
Replace that example address with your actual folder. If the folder does not exist, create it in your file manager first. A command showing Amina's example address is not a command you should expect to work unchanged on your computer.
| Task | PowerShell | macOS or Linux shell |
|---|---|---|
| Show the working folder | Get-Location | pwd |
| List its files | Get-ChildItem | ls |
| Move to a folder | Set-Location "path" | cd "path" |
| Check Python | python --version | python3 --version |
| Run a file | python chapter_labels.py | python3 chapter_labels.py |
On some Windows installations, the Python launcher is available as py instead of python. py --version and py chapter_labels.py serve the same purpose here. Choose the command that points to your intended Python 3 installation, and use it consistently until we introduce an environment manager.
Confirm the interpreter before blaming the file
The Python interpreter is the program that reads and executes Python instructions. Check that it is available:
python --version
You should see a Python 3 version number. The standard-library examples in this chapter work with Python 3.12. If the command is unknown, opens an app store or starts a different installation, the Python file has not run yet. That is a setup problem, not evidence of a bug in the function.
Use the official Python installation instructions for your system and choose a currently supported Python 3 release. Python 3.12 is the checked baseline for this chapter, not a claim about the newest release. After installing or changing a command path, open a new terminal and check again. Avoid changing several unrelated settings at once: one change followed by one check is easier to understand.
Save a file you can identify
Open a text editor such as VS Code or your system's plain-text editor. An editor lets you write and save the characters in a source file. Create chapter_labels.py inside your chosen working folder and save this complete example:
def chapter_label(number, title):
return f"Chapter {number}: {title}"
print(chapter_label(1, "Python and Roots"))
The .py ending identifies the file as Python source. Check that your editor has not saved it as chapter_labels.py.txt. Save the file before running it; otherwise you may be testing the previous version on disk.
Return to the terminal. Get-ChildItem should list that exact filename. Then run:
python chapter_labels.py
The shell starts Python and gives it the filename. Python reads the file, defines the function, calls it and displays:
Chapter 1: Python and Roots
This is the complete route from command to result. The terminal does not need to understand the function. It starts the interpreter that does.
Separate three kinds of failure
Suppose the command does not produce that result. Locate the failure before asking the AI to rewrite anything:
| What you see | What it tells you | First useful check |
|---|---|---|
The shell cannot find python | Python did not start | The interpreter command and installation |
| Python cannot open chapter_labels.py | Python started but did not find that file | Working folder, spelling and file extension |
SyntaxError with a line or caret | Python could not parse the instructions | The marked line, its punctuation and indentation |
NameError: name 'titel' is not defined | Python reached a name it cannot resolve | Whether titel was meant to be title |
Try one deliberate break in your own file: change the function's body to use titel instead of title, leaving the parameter unchanged. Predict the result, then run it.
The error should end with NameError. Above it, the traceback shows the route of calls that led to the failure, including filenames and line numbers. Start with the final exception message to learn what happened. Then use the frame and line information to find where your code contributed to it. The last frame may be inside a library, so reading only the last filename is not always enough.
Restore title and run again. If the good result returns, you have connected a specific change with a specific failure. That is much more useful than pasting an entire terminal window into an AI and accepting any rewrite it offers.
Safety floor
Before running an AI-proposed command, understand which files it reads or changes. Pause on deletion, downloads, installation, administrator privileges or secret values. Share only the relevant error lines; remove passwords, tokens and private data before asking for help.
Errors are normal evidence during development. The error message may be unfamiliar; it is still something you can narrow down. Tell the AI the command, the expected result, the relevant message and what you already checked. Ask it to explain the cause before making the smallest repair.
Check your route through the failure
From memory, answer:
- The file is open in an editor, but Python says it cannot open it. What two facts would you check first?
- Why does an unrecognised
pythoncommand tell you nothing about the function's correctness? - What do you read in a traceback before deciding which line to inspect?
- What evidence would show that repairing
titelactually fixed the intended failure?
Compare after you have answered
Check the working folder and the saved filename, including its extension. An unrecognised interpreter command means Python never executed the file. Read the exception type/message and then the relevant call frames. Repairing the typo should remove that failure and restore the predicted chapter label; merely hiding the message is not a fix.
The Python guide to errors explains the error and traceback forms. PowerShell's Get-Location documents the working-folder command.
You can now run one function and investigate its failure. In the next lesson, the function gains decisions and collections, so there is more than one possible path to predict.