ProjectStack
python

SyntaxError: invalid syntax

Python found a line it couldn't parse because it violates Python's grammar rules. The error points to where Python got confused, but the real mistake is often one line earlier.

Common causes

  • A missing colon at the end of an if, for, while, def, or class statement
  • Mismatched parentheses, brackets, or quotes — an opening one was never closed
  • Using Python 3 syntax in Python 2 (or vice versa), such as print 'hello' instead of print('hello')
  • A typo like = instead of == in a condition, or an extra/missing comma

How to fix it

  1. Look at the line the error points to AND the line immediately before it
  2. Check that every opening bracket (, [, { has a matching closing one
  3. Make sure every if/for/while/def/class line ends with a colon :
  4. Run python --version to confirm you're using the right Python version

Example

File "app.py", line 5 if x == 1 ^ SyntaxError: invalid syntax

A missing colon at the end of an if statement

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.