Python "SyntaxError" under a newer Python in CI
Code that parses on your local Python raises a SyntaxError on the CI runner because the interpreter version differs. A feature you rely on may be unavailable, or a deprecated construct is now rejected.
What this error means
CI fails at import/collection with "SyntaxError" pointing at valid-looking code, while it runs cleanly on a developer machine.
python
File "app/handlers.py", line 12
match command:
^
SyntaxError: invalid syntaxCommon causes
A feature missing on the runner Python
Syntax like structural pattern matching (3.10+) or except* (3.11+) raises a SyntaxError on an older CI interpreter.
A removed construct on a newer runner Python
A newer CI Python rejects something removed since 2->3 era code, like a bare print statement.
How to fix it
Pin the CI Python to the supported version
- Identify the syntax and the minimum/maximum Python it requires.
- Set the runner interpreter with setup-python to a supported version.
- Test across the full version matrix you claim to support.
.github/workflows/ci.yml
- uses: actions/setup-python@v5
with:
python-version: '3.11'Match the matrix to your declared support
Run CI on every Python you advertise in requires-python so a version-specific syntax error cannot slip through.
.github/workflows/ci.yml
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']How to prevent it
- Test on every Python version listed in
requires-python. - Pin the CI interpreter explicitly with setup-python.
- Keep
requires-pythonhonest about features you use.
Related guides
Wrong Python Version in CI - Pin the InterpreterFix the wrong Python version running in CI - syntax features failing or wheels missing because PATH resolves…
Python "ModuleNotFoundError: No module named X" import in CIFix "ModuleNotFoundError: No module named X" in CI - the import target is not installed in the active environ…
Python "ImportError: cannot import name X" in CIFix "ImportError: cannot import name X from Y" in CI - the module exists but the named attribute is missing,…