SyntaxError in Python — What It Means and How to Fix It

What Python's SyntaxError exception means and how to resolve it.

Published October 27, 2026

SyntaxError is raised when Python cannot parse your code, so it will not run. The pointer in the message shows where parsing failed.

What it means and why it happens

Typical causes: missing colons, unmatched brackets or quotes, using reserved words as names, or newer syntax on an older Python.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
SyntaxError: invalid syntax

Practical examples

Code that triggers it

if x == 1
    print('one')

The fix

if x == 1:
    print('one')

Check the Python version

python --version
python -m py_compile file.py

Common mistakes

  • Missing colon after if/for/def.
  • Unclosed brackets (error reported later).
  • Using = instead of == in conditions.

Key takeaways

  • SyntaxError: SyntaxError is raised when Python cannot parse your code, so it will not run.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Why is the error on the next line?

Python reports where parsing failed, often after an unclosed bracket.

How do I find which line caused SyntaxError?

The error message and stack trace show the file and line; start there and work backwards.

More Python articles