Fatal error: Python.h: No such file or directory

The "Python.h: No such file or directory" error is encountered when attempting to build applications using GNU Compiler Collection (gcc) and the Python.h header file is missing. To resolve this error, you should install the "python-dev" package, which provides the necessary files for building Python modules, extending the Python interpreter, or embedding Python in applications.

The "python-dev" package includes header files, a static library, and development tools essential for successful compilation. If you encounter this error while trying to build a shared library using the file extension of another language (e.g., 'C'), it indicates that you need to install the appropriate development version of Python to ensure compatibility with your desired language and proper linkage to the Python interpreter.

Reason for this error:

  1. You haven't properly installed the header files and static libraries for python dev.
  2. Also, sometimes include files might not be default in the include path.

How to solve this error:

  1. Install the missing files and libraries.
  2. Include Path and Library.
  3. Finally, Compile it.

Python.h: No such file or directory

Install the missing files and libraries

To resolve the issue of missing Python development files, it is essential to install them on your operating system, especially if the Python version bundled with your OS does not include them by default. To achieve this, use the appropriate commands based on the operating system installed on your system. For instance, on Debian or Ubuntu, you can use "sudo apt-get install python3-dev" to install the necessary Python development files. On Red Hat or CentOS, you can use "sudo yum install python3-devel" for the same purpose. Use the following commands according to the OS installed on your system.

Ubuntu, Debian (apt)

sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs

CentOS, RHEL (yum)

sudo yum install python-devel # for python2.x installs sudo yum install python3-devel # for python3.x installs

Fedora (dnf)

sudo dnf install python2-devel # for python2.x installs sudo dnf install python3-devel # for python3.x installs

openSUSE (zypper)

sudo zypper in python-devel # for python2.x installs sudo zypper in python3-devel # for python3.x installs

Alpine (apk)

# This is a departure from the normal Alpine naming # scheme, which uses py2- and py3- prefixes sudo apk add python2-dev # for python2.x installs sudo apk add python3-dev # for python3.x installs

Cygwin (apt-cyg)

apt-cyg install python-devel # for python2.x installs apt-cyg install python3-devel # for python3.x installs

Rasberry Pi (apt)

sudo apt-get install python-dev

Include Path and Library

If you are not already include path and Library, use include files in the default include path and Python Library to be linked with executable.

-I/usr/include/python2.7 -lpython2.7
-I/usr/include/python2.7 -lpython3.7

Finally, Compile it

gcc -Wall -I/usr/include/python3.7 -lpython3.7 utilsmodule.c -o Utilc
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc

How to ensure the Python dev files come with your OS?

The "pkg-config" tool serves as a valuable aid during the compilation of applications and libraries. To prevent encountering the mentioned error, it is crucial to ensure that the Python development files are present on your operating system. Avoid hardcoding the library and include paths to circumvent any fatal errors. Instead, utilize "pkg-config" to dynamically insert the appropriate compiler options on the command line, thus enabling applications to utilize the necessary libraries and flags seamlessly. For instance, you can employ "gcc -o test test.c pkg-config --libs --cflags glib-2.0" to benefit from "pkg-config" capabilities.

$ pkg-config -- cflags --libs python2-I/usr/include/python2.7 -lpython2.7

Also, add the following to the gcc line:

gcc $(pkg-config --cflags --libs python2) -Wall utilsmodule.c -o Utilc


The error message "Fatal error: Python.h: No such file or directory" indicates that the Python header file "Python.h" is missing or not found in the specified location. This error often occurs during the compilation of applications or libraries that require Python development files, and it can be resolved by installing the appropriate Python development package for the system or ensuring the correct path to the "Python.h" file.