Skip to content
Latchkey

Python "SyntaxError" from Mixed Versions in CI

A SyntaxError at parse time usually means the interpreter is older than the syntax you wrote - match statements, the walrus operator, or f-strings on a too-old Python - or a stray Python 2 idiom.

What this error means

The program fails before any code runs, pointing a caret at valid-looking syntax. This is a parse-time error: the interpreter cannot even compile the file, so it never executes.

Python traceback
  File "handler.py", line 14
    total := compute()
          ^^
SyntaxError: invalid syntax

Common causes

New syntax on an old interpreter

The walrus operator (3.8+), match (3.10+), and exception groups (3.11+) all raise SyntaxError on older Pythons that cannot parse them.

Python 2 vs 3 idioms

A print statement, old except X, e: syntax, or integer division assumptions raise SyntaxError when run under Python 3 (or vice versa).

How to fix it

Run on the Python the syntax requires

  1. Identify the minimum Python version for the syntax (e.g. match → 3.10).
  2. Pin CI to that version or newer with actions/setup-python.
  3. Confirm with python --version in the same job.

Fix the offending construct

This is genuine source code, not a transient failure - correct the syntax or guard it behind a version check rather than retrying.

How to prevent it

  • Pin a Python version that supports every syntax feature you use.
  • Run a lint/compile step (python -m py_compile) early in CI.
  • Set requires-python to your true minimum so installs fail clearly on old Pythons.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →