SyntaxError: invalid syntax
Python couldn’t parse the code. The caret (^) points at or just after the mistake.
Seen on:
Python
Meaning
Missing colons, unclosed brackets on the previous line, using = instead of ==, Python 2 print statements and keywords used as names are common causes. Newer Pythons give more precise messages (“expected ':'”).
Common causes
- Missing : after if/for/def
- Unclosed ( [ { on the line above
- print "x" (Python 2 syntax)
- Running new syntax (match, walrus) on an old Python
- Pasting shell commands (pip install) into the Python REPL
⚡ Quick fix
- Look at the line above the caret
- Check Python version (python --version)
- Run pip from the terminal, not inside python
Detailed fix by platform
Python
- python -m py_compile file.py checks syntax without running
How to diagnose
- Caret — Where does ^ point?
- Previous line — Unclosed bracket?
- Version — Syntax supported?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026