How do you fix "fatal error: Python.h: No such file or directory" when compiling Python packages? | A1Lab@MZeeshanM to PythonEnglish • 5 days ago

How do you fix ""fatal error: Python.h: No such file or directory"" when compiling Python packages?

message-square
0

Install the Python C development headers package matching your exact python3 minor version—such as sudo apt install python3-dev (or python3.12-dev) on Debian/Ubuntu, sudo dnf install python3-devel on Fedora/RHEL, or apk add python3-dev on Alpine—alongside a C compiler toolchain (build-essential / gcc), because pip is building a C/C++ extension from source (sdist) and cannot find Python’s C API header files (Python.h).

Basic Example

# 1. Debian / Ubuntu / WSL: Install headers for default python3 AND your exact minor version
sudo apt update
sudo apt install -y build-essential python3-dev

# If using a non-default Python version (e.g., Python 3.12 from deadsnakes PPA):
sudo apt install -y python3.12-dev

# 2. Fedora / RHEL / Rocky / AlmaLinux:
sudo dnf install -y gcc python3-devel

# 3. Alpine Linux (minimal Docker containers):
apk add --no-cache build-base python3-dev

# 4. Retry installing your package (and upgrade pip first to prefer prebuilt wheels)
python3 -m pip install --upgrade pip
python3 -m pip install <package-name>

Key Details & Edge Cases

  • Match the -dev / -devel package to your active python3 --version: On Ubuntu/Debian, sudo apt install python3-dev only installs headers for the system’s default Python (e.g., Python 3.12 on Ubuntu 24.04). If your virtual environment runs Python 3.11 or 3.13, Python.h will still be missing until you install python3.11-dev or python3.13-dev specifically.
  • Why pip tries to compile from source in the first place (and how to avoid compiling altogether):
    • pip only invokes GCC and needs Python.h when no precompiled binary wheel (.whl) exists for your OS, CPU architecture (e.g., aarch64 / ARM64), or brand-new Python version—or when your pip binary is too old to recognize modern manylinux wheels.
    • Run python3 -m pip install --upgrade pip setuptools wheel first, or pass --only-binary=:all: to force pip to download a prebuilt wheel instead of falling back to C compilation.
  • Custom or source-compiled Python builds (pyenv, uv, Conda):
    • pyenv: Headers are compiled automatically during pyenv install 3.x.y, provided system build dependencies (zlib1g-dev, libssl-dev, etc.) were present beforehand.
    • Conda / Mamba: Headers ship inside the active Conda environment ($CONDA_PREFIX/include/python3.x/Python.h); ensure you are using the environment’s pip (python -m pip install ...) rather than a system pip.
  • Verifying where Python expects Python.h to live: Check the exact include directory your active interpreter queries during C compilation:
    python3 -c "import sysconfig; print(sysconfig.get_path('include'))"
    # Output e.g.: /usr/include/python3.12 (verify Python.h exists inside this folder)
    
  • Missing library-specific C headers (ffi.h, openssl/ssl.h, pg_config): Once Python.h is resolved, compiling packages like cryptography, cffi, or psycopg2 may require companion C libraries (libffi-dev, libssl-dev, libpq-dev)—or can be bypassed with binary wheels (e.g., pip install psycopg2-binary).

Platform & Package Manager Reference

Operating System / Environment Command to Install Python.h & C Compiler Header Location
Ubuntu / Debian / WSL sudo apt install -y build-essential python3-dev (or python3.X-dev) /usr/include/python3.X/Python.h
Fedora / RHEL / CentOS / Rocky sudo dnf install -y gcc python3-devel (or python3.X-devel) /usr/include/python3.X/Python.h
Alpine Linux (Docker) apk add --no-cache build-base python3-dev /usr/include/python3.X/Python.h
openSUSE sudo zypper install -y gcc python3-devel /usr/include/python3.X/Python.h
macOS xcode-select --install (headers included with Python.org / Homebrew) /Library/Frameworks/... or /opt/homebrew/...
Windows Install Microsoft C++ Build Tools (headers included in python.org installer) C:\Users\<user>\AppData\Local\Programs\Python\Python3X\include\Python.h