Python's error messages are not a punishment. They are the closest thing your program has to an explanation: what failed, where it failed, and sometimes the exact character it could not make sense of.
The useful habit is reading that explanation before changing the code. Closing the message and running the same file again is not a debugging method.
First, work out whether Python understood the code
The Python tutorial separates syntax errors from exceptions. A syntax error means Python could not parse your program. An exception means it understood the instructions, started executing them, and hit a problem.
That distinction gives you somewhere to start. If execution never began, look at the structure. If it stopped partway through, look at the names, values and operations involved.
A SyntaxError can point past the actual mistake
A missing colon after a while statement can make Python complain about what comes next. The arrow shows where the parser detected a problem, which is not necessarily where you introduced it.
The arrow marks the discovery, not always the mistake.
When you see SyntaxError: invalid syntax, read the previous line too. Check unmatched brackets, missing colons and commas. An unterminated string literal usually means an opening quote has no matching closing quote. A suggestion that you forgot a comma is exactly that: a suggestion worth checking, not an instruction to insert commas at random.
Indentation is code, not decoration
IndentationError is a kind of syntax error. Python uses indentation to decide which statements belong together.
expected an indented block means a statement opened a block but nothing was indented underneath it. unexpected indent means a line moved inward where no new block was expected. unindent does not match any outer indentation level means the line moved back to a level Python cannot match.
TabError is the particularly annoying relative. Tabs and spaces can look aligned while representing different indentation. Set your editor to use four spaces and make the file consistent. Deleting all the whitespace is not the fix.
NameError means the name is missing, not necessarily the package
NameError: name 'pd' is not defined tells you Python cannot find that name in the current scope. The same goes for plt and np, familiar aliases that do not exist until your code defines them.
Check for a missing import, a typo, or a cell you forgot to run. Installing pandas does not, by itself, create a variable called pd. You still need the appropriate import in the code you are executing.
UnboundLocalError is more specific: a function is trying to read a local variable before assigning it a value.
TypeError tells you which kinds of data collided
'2' + 2
One value is text; the other is an integer. Python will not guess whether you want arithmetic or text concatenation. The resulting TypeError tells you the incompatible types.
Decide what the operation means before converting anything. For arithmetic, convert the text to a number. For a message, convert the number to text or use an f-string. Making the error disappear is not enough if the result means something different.
A package can be installed and still be missing
ModuleNotFoundError: No module named 'X' means the interpreter running your import cannot find that module. It does not prove the package is absent everywhere on your computer.
A virtual environment has its own installed packages. Your editor may run one interpreter while the terminal's pip installs into another. Check which Python runs the file and install into that environment. Using that interpreter's python -m pip helps keep the two connected.
Read the traceback from the bottom, then follow the route
The last line normally gives you the exception and its message. Start there. Then inspect the file and line immediately above it, following the calls back until you reach your own code.
If the file is 200 lines long, reduce the problem to the smallest example that still fails. Keep the actual error message. Look up the exception in the documentation. Change one thing for a reason, then run it again.
Random edits can stack a second mistake on top of the first. Reading the message takes less time than untangling both.
Related reading: the single most common one has its own walkthrough in ModuleNotFoundError: pip Said Success, and the first program worth running is in Your First Tkinter Window Is Five Lines Away.

Comments
Post a Comment