Problem Statement: How to fix “fatal error: Python.h: No such file or directory“?
What is a “fatal” error?
A fatal error causes a program to end with practically no warning without even saving its state. It usually occurs when an application attempts to access a piece of invalid information or data. The program closes down as it shows illegal action, and it returns the user to the operating system. When the fatal error occurs, the user can lose any unsaved changes made in the program.
fatal error: Python.h: No such file or directory
When does the error occur?
Generally, you face this error when you try to generate the output file while building a shared library using a C extension file. In other words, you encounter this error while trying to build a shared library using the file extension of another language( e.g. C ).
Example: Suppose you use the command below.
gcc -Wall utilsmodule.c -o Utilc
When you execute the above command, you will get the following error message:
utilsmodule.c:1: 20: fatal error: Python.h: No such file or directory compilation terminated.
Now that you know the reasons behind the occurrence of the error let’s look at the ways to fix it.
#Fix 1: Using Package Managers According to Your Operating System
Most probably, when you face this error, it’s because you have not installed the static libraries and the header files properly. Hence to solve the error, you need to use the package manager to install them on the PC.
Use the following commands according to the OS installed on your system.
sudo apt-get install python-dev
sudo apt-get install python3- dev
sudo dnf install python-devel
sudo dnf install python3- devel
sudo yum install python-devel
sudo yum install python3 – devel
apt-cyg install python-devel
apt-cyg install python3- devel
sudo zypper in python- devel
sudo zypper in python3 -devel
sudo apk add python-dev
sudo apk add python3 -dev
Caution: The above command only works if you are using either Python 2 or Python 3 version.
The commands for Python 3.6, 3.8, and 3.9 versions are as follows:
sudo apt install libpython3.6 -devsudo apt install libpython3.8 -devsudo apt install libpython3.9- dev
#Fix 2: Ensure that the Python Dev Files Come with Your OS
You can solve this error by checking that the Python dev files come with your operating system. To avoid the fatal error, you should not hard code the library and include paths. However, you can use the pkg-config that will output the correct options for the specific system:
$ 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
#Fix 3: By Changing the Header’s Directory
Sometimes you can solve the “
fatal error: Python.h: No such file or directory” by simply making changes to the header files. These files are typically installed with Python.
Locating the header files
On Unix os the header files are located in the directories
exec_prefix/ include/pythonversion/. Here the prefixes are defined using the parameters to the configure script in Python, and the version is
'%d.%d' % sys.version_info[:2]. On Windows, the files are installed in
prefix/include, where prefix is the installation directory that is specified to the installer.
To find the header file itself, you can use Python 3 configuration:
$ python3-config --include-I/Users/<username>/.pyenv/versions/3.8.2/include/python3.8
Generally, the Python development headers are located in the directory shown.
To avoid the error, you must include the headers by placing both the directories on your compiler’s search path for includes. So instead of using the following header directory, which results in a fatal error:
Just change the header directory. You can even change it with the following:
#include <python2.7/ Python.h>
If you are using a virtual environment tool such as venv, then most probably, the Python development headers will already be included in compilation and the linking by default. If it’s not, then you will get an error message:
fatal error: 'Python.h ' file not found #include <Python.h>
If this happens, you must ask the setup.py to search for the header files by setting the
CFLAGS. You can now locate the header files and use them with Python
$ CFLAGS = "$(python 3- config -- include)" python setup.py bdist_wheel $ CFLAGS = '-I/path/to/include' python setup.py bdist_wheel
In this article, we learned how to solve the
fatal error:Python.h: No such file or directory. I hope this discussion helped you to solve your problem. Please stay tuned and subscribe for more interesting solutions and discussions in the future. Happy learning!
- One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
- So, do you want to master the art of web scraping using Python’s BeautifulSoup?
- If the answer is yes – this course will take you from beginner to expert in Web Scraping.