DevOps | Cloud | Analytics | Open Source | Programming





How To Fix - fatal error: Python.h: No such file or directory ?



This post would explain a fix as regards to How To Fix - fatal error: Python.h: No such file or directory.

Issue:


  • While building shared library using other language file extension ( e.g. C ) , we sometimes get the below error -
 


utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated.

 

Reason:


  • This error occurs when we miss to install all the Python Developement files (header files and static libraries)

  • Also sometimes include files might not be default in the include path. Or Python library linked with executable by default. We might have to add these flags (using Correct Python's version)

 

Fix:


 

Step 1: Install Dev Packages

The obvious solution is to to install the missing files and libraries as explained below. We will see what to install the files for different operating systems -  

1. Ubuntu


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

2. Linux Fedora


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

3. Red Hat Linux


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

 

4. SUSE Linux


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

 

5. CentOs


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

 

6. Debian


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

7. If using Cygwin console


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

8. Rasberry Pi


sudo apt-get install python-dev

 

  • Use include files in the default include path and Python Library to be linked with executable , if not already

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

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

 

Step 3 :  Compile


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

  Hope this helps to fix the fatal error in Python.   Additional Posts: