×

Search anything:

[FIXED] fatal error: Python.h: No such file or directory

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we have explored the reason behind the compilation error "fatal error: Python.h: No such file or directory" and presented 3 different fixes to resolve this error.

Table of contents:

  1. Reason for error
  2. Fix 1: Install python-dev
  3. Fix 2: Add include header
  4. Fix 3: Update compilation command

Reason for error

During compilation of a C or C++ program, you may face the following compilation error:

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

The reason is that the compiler is not able to locate the required header file Python.h.

The compilation command is usually as follows:

gcc code.c -o code

Fix 1: Install python-dev

The first fix is to re-install python-dev and python3-dev packages in Linux which will reinstall the header files and static/ dynamic libraries in the correct location and fix the error. Use these commands:

sudo apt-get install python-dev
sudo apt-get install python3-dev

If it does not fix, try installing libpython3.10-dev using the following command:

sudo apt install libpython3.10-dev

Note: change the Python version in the above command accordingly.

If you are using Fedora, use the following command:

sudo dnf install python2-devel
sudo dnf install python3-devel

If you are using OpenSUSE, use the following command:

sudo zypper in python-devel
sudo zypper in python3-devel

If you are using CentOS or RHEL, use the following command:

sudo yum install python-devel
sudo yum install python3-devel

If you are using Alpine, use the following command:

sudo apk add python2-dev
sudo apk add python3-dev

Fix 2: Add include header

If the header file is in the path /usr/include/python3.10/Python.h, add the following line in your C/ C++ code:

#include <python3.10/Python.h>

We need not specify the entire path as /usr/include is in the INCLUDE path by default.

If you are not sure where the header file is located in your system, use the following command to locate it:

pkg-config --cflags --libs python

If it works correctly and the header file is present in your system, you will get an output like:

-I/usr/include/python3.10 -lpython3.10

Fix 3: Update compilation command

Locate where Python is installed in your system and use the following flags in your compilation command:

-I/usr/include/python3.10 -lpython3.10

This will help the compiler to identify the header files. Following is the updated compilation command:

gcc -I/usr/include/python3.10 -lpython3.10 code.c -o code

Use the following command to locate it where the header file is in your system:

pkg-config --cflags --libs python

If it works correctly and the header file is present in your system, you will get an output like:

-I/usr/include/python3.10 -lpython3.10

We can use the command to get the path directly in the compilation command:

gcc code.c -o code $(pkg-config --cflags --libs python) 

With this article at OpenGenus, you must have fixed this error by applying the fixes we presented. Continue with your development.

Geoffrey Ziskovin

Geoffrey Ziskovin

Geoffrey Ziskovin is an American Software Developer and Author with an experience of over 30 years. He started his career with Haskell and has interviewed over 700 candidates for Fortune 500 companies

Read More

Improved & Reviewed by:


OpenGenus Tech Review Team OpenGenus Tech Review Team
[FIXED] fatal error: Python.h: No such file or directory
Share this