Wrong Python Version in CI - Pin the Interpreter
Your job is running on a different Python than you think. A newer syntax fails on an old interpreter, or a dependency’s wheel is missing because PATH points at an unexpected version.
What this error means
Code that works locally fails in CI with a SyntaxError for a feature your version supports, or installs the wrong wheels. python --version in CI prints something other than what you intended.
$ python --version
Python 3.8.10
$ python app.py
File "app.py", line 10
match command:
^
SyntaxError: invalid syntaxCommon causes
The default system Python is older than expected
The runner image’s default python3 may be older than your code requires. Without pinning, you inherit whatever the image ships.
setup-python not used or shadowed
If actions/setup-python is missing, or a later PATH change shadows it, the interpreter in use is not the one you configured.
How to fix it
Pin the interpreter explicitly
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: python --version # assert it's what you expectTest a matrix of versions
If you support several versions, run them all so a version-specific break is caught.
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']How to prevent it
- Always pin the Python version in CI; never rely on the image default.
- Add an early
python --versionassertion step. - Declare
requires-pythonso consumers get a clear error on mismatch.