Fatal error: Python.h: No such file or directory
Python.h is used by GNU Compiler Collection (gcc) to build applications. You need to install a package called python-dev 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. You encounter "Python.h: No such file or directory" error while trying to build a shared library using the file extension of another language ( e.g. 'C' ). If you are trying to build a shared library using the file extension of another language, you need to install the correct development version of Python.Reason for this error:
- You haven't properly installed the header files and static libraries for python dev.
- Also, sometimes include files might not be default in the include path.
How to solve this error:
- Install the missing files and libraries.
- Include Path and Library.
- Finally, Compile it.

Install the missing files and libraries
You must install the Python development files on your operating system if the Python provided with your operating system does not come with them. 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 is a helper tool used when compiling applications and libraries. You can easily avoid 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. The pkg-config helps you insert the correct compiler options on the command line so an application can use gcc -o test test.c 'pkg-config --libs --cflags glib-2.0'. example
$ 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
Related Topics
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- IndentationError: unexpected indent Error
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3