How to fix the “Python.h: No such file or directory” error

When writing a program using a certain language you might come across the need to import libraries or extensions from various languages. One of these libraries is the “Python.h” library. It is a header file that is commonly added to C or C++ code which helps to embed Python code into the other language.

Running this header file in the code is a little tricky and can lead to the error “Python.h: No such file or directory”. This article will elaborate on the causes behind this error and what we can do to fix it.

How to fix the error “Python.h: No such file or directory”?

Including extensions from another language is slightly complex. There are various different reasons that can invoke the error mentioned above. Let’s have a look at some of the major causes and their solutions.

Reason 1: Bugged or missing Python-dev library

The most obvious reason that is invoking this error refers to a fault in the header files that you have installed on your system. It is likely that they are not working or have become corrupted. When this happens, your code cannot locate the library on your system and it will prompt the “Python.h: No such file or directory” error.

Solution

The solution to the issue stated above is straightforward. If the Python-dev library is bugged or missing, reinstalling it on your system will easily fix the issue. You can choose one of the following methods to install the Python-dev library as per your system:

For Ubuntu/Debian

Run this statement in your terminal to install the library on Ubuntu/Debian system:

$ sudo apt install python3-dev

For Fedora

Use this command to install the library on your Fedora system:

$ sudo dnf install python3-devel

For CentOS

Use this command to install the library on your CentOS system:

$ sudo yum install python3-devel

Reason 2: Library path not located

Another reason that will cause this error to pop up is that the program is not able to locate the header file with its correct path. When this happens, the code throws a “Python.h: No such file or directory” problem. Take a sample code as shown below in the image:

Use the below-mentioned command to run the code:

$ gcc sample.c

This command will compile the code but will be unable to locate the python library and resulting in the error shown below:

Solution

To solve the issue of locating the Python.h file, it is important to let the compiler know where Python is located. This can be done using these steps.

Step 1: Find the location of Python.h

To find the location of your Python file uses the following command:

Step 2: Compile using the location

Once you know the location, use it to execute this command:

$ gcc -I /usr/include/python3.10/ sample.c 

Where sample.c is your file and the rest is your path. Through these steps, your compiler will be able to locate the library and successfully compile your program.

Conclusion

The two main causes for the “Python.h: No such file or directory” error are missing the library and the compiler being unable to locate the library. To solve them you can either install the Python-dev onto your system or you can help the compiler in locating the library from the system.