Home › Python

fatal error: Python.h: No such file or directory — how to fix

src/module.c:1:10: fatal error: Python.h: No such file or directory
    1 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.
error: command '/usr/bin/gcc' failed with exit code 1

Quick fix: Install the headers for the Python version you're building with: Debian/Ubuntu sudo apt install python3-dev (or python3.12-dev for a specific version), Fedora/RHEL sudo dnf install python3-devel, Alpine apk add python3-dev. Also install build-essential/gcc. Often you can avoid compiling entirely by upgrading pip so it finds a prebuilt wheel.

What it means

Python.h is the main header of the Python C API. Any package with C/C++ code (or Cython) includes it when it is built from source. On Linux distributions the interpreter and its headers are split: the python3 package lets you run Python, while python3-dev (Debian/Ubuntu) or python3-devel (Fedora/RHEL) adds the headers and build files. Without them, gcc stops at the first #include <Python.h>.

You usually see it inside a pip install that ends with Failed building wheel for ....

Fix 1: install the development headers

Debian / Ubuntu:

sudo apt update
sudo apt install python3-dev build-essential

Fedora / RHEL / Rocky / Alma:

sudo dnf install python3-devel gcc

Alpine (also common in Docker):

apk add --no-cache python3-dev gcc musl-dev

Arch includes the headers in the normal python package; just make sure base-devel is installed.

Fix 2: match the headers to the Python version you use

The headers must belong to the interpreter that runs pip. Check it:

python3 -m pip --version     # shows which python pip belongs to
python3 -c "import sysconfig; print(sysconfig.get_paths()['include'])"

If you use a non-default version on Ubuntu (for example from the deadsnakes PPA), install the matching package:

sudo apt install python3.12-dev

On RHEL-family systems with several versions, the package is named like python3.11-devel.

If the include directory printed above exists and contains Python.h, the headers are fine and the build is using a different interpreter or a broken compiler setup.

Fix 3: official Docker python images

The python:3.x and python:3.x-slim images already include the headers, because Python is built from source in them. On -slim you are more likely missing the compiler:

FROM python:3.12-slim
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
    && rm -rf /var/lib/apt/lists/*

If you installed Python with apt-get install python3 on a debian/ubuntu base image instead, add python3-dev.

Fix 4: avoid compiling at all

Many packages publish prebuilt wheels; pip only compiles when it can't find one for your platform and Python version. An old pip may not recognise newer wheel tags:

python3 -m pip install --upgrade pip
python3 -m pip install <package>

If there is still no wheel, the package may not support your Python version yet. Using a Python version the package does support, or a newer release of the package, often makes the build step disappear.

Fix 5: pyenv, conda and virtualenvs

Last updated 2026-12-02.