Python Reference ยท Exceptions
Python SyntaxError and IndentationError
Reference for Python SyntaxError, IndentationError and TabError: common causes like missing colons and brackets, error attributes, and catching them with compile().
SyntaxError is raised when Python cannot parse your code: a missing colon, an unclosed bracket, an unterminated string, an invalid assignment. It happens before any line of the file runs, so a syntax error anywhere in a file prevents the whole file from starting.
IndentationError is a subclass for bad indentation (unexpected indent, missing indented block, a dedent that matches no outer level), and TabError is a subclass of that for mixing tabs and spaces ambiguously. Python 3 also reports the line and a caret under the problem spot.
if x > 3 # SyntaxError: expected ':' print(x)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
try: code = compile(source_text, "<string>", "exec") except SyntaxError as error: print(error.msg, error.lineno, error.offset, error.text)
| Attribute | Description |
|---|---|
msg |
The short explanation, such as expected ':'. |
filename |
The file name (or the name passed to compile()). |
lineno |
The line number where the problem was detected. |
offset |
The column (1-based) where it was detected. |
text |
The source line. |
end_lineno, end_offset |
The end of the highlighted range (3.10+). |
Raises when: the tokenizer or parser rejects the source: at import or run time of a file, and in compile(), exec(), eval() and ast.parse() on strings.
Usage notes
- Look at the line before. Python reports where it noticed the problem, which can be the line after an unclosed bracket or quote. A missing
)often makes the next line "wrong". - Frequent causes: missing
:afterif,for,def,class; unmatched brackets or quotes;=where==is meant in a condition; a keyword used as a name (class = 3); Python 2 syntax such asprint "x"; f-string expressions with the wrong quotes in older versions. - Indentation. Use four spaces per level and never mix tabs and spaces. Configure the editor to insert spaces. A
TabErrormeans the mixing makes the meaning ambiguous. - Messages improve with each release. Python 3.10 added precise messages (
'(' was never closed) and 3.13 added colored tracebacks, so the same mistake reads differently across versions. - Catching it is for dynamic code. Use
try/except SyntaxErroraroundcompile(),eval()orexec()of text a user typed. For your own source, fix the file. - Prefer safer tools than
eval().ast.literal_eval()parses Python literals only and raisesValueErrororSyntaxErroron anything else; nevereval()untrusted text. - Importing a file with a syntax error raises
SyntaxErrorfrom theimportstatement, with the offending file named in the traceback. - Warnings are separate.
SyntaxWarning(for example"is"with a literal) is reported at compile time but does not stop the program. - Run
python -m py_compile file.pyor let your editor lint the file to find problems before running.
Related
NameError,ValueError, the exception hierarchycompile(),eval(),exec()- Lessons: Syntax, Debugging