← Debugging Guides

阅读中文版本

Python SyntaxError: invalid syntax

Fix Python SyntaxError by reading the caret location, checking recent edits, and remembering that Python 2 print and async differences still bite.

beginnerdebuggingpythonsyntax

Symptoms

  • SyntaxError: invalid syntax with a caret under a token
  • The reported line looks correct, but the previous line is the real culprit
  • Code copied from a tutorial fails with syntax errors

Possible causes

  • A missing parenthesis, bracket, or colon on the previous line
  • Using print as a statement or async as an identifier
  • An f-string or walrus operator used on an older interpreter
  • Pasting code with smart quotes or non-breaking spaces from a document

Diagnosis steps

Read the caret and check the line above

Interpretation

Python often flags the token after the actual mistake, so a missing ) or : appears on the preceding line.

Show invisible characters

python -c "import pathlib; print(repr(pathlib.Path('f.py').read_text()[:200]))"

Interpretation

Smart quotes and non-breaking spaces are invisible in editors but illegal to Python.

Confirm the interpreter version and grammar

python --version

Interpretation

Walrus :=, f-strings, and async syntax require specific versions; older interpreters reject newer grammar.

Root cause

The parser failed before execution, so nothing about runtime values matters. The caret marks where parsing got confused, which is frequently one token past an unclosed delimiter, a stray character, or a construct the running interpreter version does not support.

Fix

Balance delimiters on the preceding lines, replace smart quotes with ASCII quotes, and remove non-breaking spaces. Use print(...) as a function call, avoid reserved words as names, and either upgrade the interpreter or rewrite newer syntax for the version you must target.

Verify the fix

Compile the file without running it to confirm the syntax is clean: python -m py_compile file.py. A zero exit status means the parser accepts the file.

Symptoms

The interpreter refuses to start with SyntaxError: invalid syntax and draws a caret. The marked line often looks fine, which is the key clue.

Diagnosis

Look one line above the caret and inspect raw bytes:

python -m py_compile app.py
python -c "print(repr(open('app.py').read()[:120]))"

The repr exposes “smart quotes” and non-breaking spaces that editors render invisibly.

Fix

Close every delimiter and use ASCII quotes:

# before: if (x > 0   (missing paren) and a stray “quote”
if (x > 0) and flag:
    print("ok")

Ensure the constructs you use are supported by python --version.

Verification

Run python -m py_compile app.py; it exits 0, meaning the parser accepts the file, then execute it to confirm behaviour.

Editor vs Interpreter

Some editors auto-insert curly quotes or strip indentation on paste, and the code looks correct on screen while the file bytes differ. When a SyntaxError survives visual inspection, diff the file against a known-good copy with python -c "print(repr(open('app.py').read()))" and read the actual bytes. Replacing curly quotes with straight ones and re-indenting with spaces rather than tabs clears the majority of these cases, and python -m py_compile gives a fast pass or fail without importing side effects.