pip "fatal error: Python.h: No such file or directory" in CI
A C extension include line #include <Python.h> could not be resolved because the Python development headers are not installed. The compiler aborts with a fatal error.
What this error means
The wheel build fails with "fatal error: Python.h: No such file or directory" during compilation, typically on slim images that have the interpreter but not its dev package.
pip
building 'simplejson._speedups' extension
src/_speedups.c:1:10: fatal error: Python.h: No such file or directory
1 | #include <Python.h>
| ^~~~~~~~~~
compilation terminated.Common causes
The python3-dev package is missing
The runtime interpreter does not include Python.h; the headers ship separately in python3-dev / python3-devel.
Headers do not match the active interpreter
Installing python3-dev for the system Python while building against a different version (pyenv, setup-python) leaves the headers unmatched.
How to fix it
Install the matching dev headers
- Install the dev package for the exact Python version you build against.
- Re-run the build so the compiler finds Python.h.
Terminal
# Debian/Ubuntu - match your version, e.g. 3.12
sudo apt-get install -y python3.12-dev
# Fedora/RHEL
sudo dnf install -y python3-develAvoid the compile with a prebuilt wheel
A manylinux wheel bundles the compiled extension and needs no headers.
Terminal
pip install --only-binary=:all: simplejsonHow to prevent it
- Install the version-matched
python3.X-devpackage before compiling extensions. - Prefer binary wheels for compiled dependencies.
- Keep dev headers aligned with the interpreter you actually build against.
Related guides
pip "error: command 'gcc' failed: No such file or directory" in CIFix "error: command 'gcc' failed: No such file or directory" in CI - the runner has no C compiler installed,…
pip "Error: pg_config executable not found" (psycopg2) in CIFix "Error: pg_config executable not found" building psycopg2 from source in CI - the PostgreSQL client heade…
pip "Building wheel for X did not run successfully" (Cython) in CIFix "Building wheel for X did not run successfully" with a Cython error in CI - the .pyx compilation step fai…