[Solved-2 Solutions] fatal error: Python.h: No such file or directory



Error Description:

  • When we try to build a shared library using a C extension file but first we have to generate the output file using the command below:
gcc -Wall utilsmodule.c -o Utilc 
click below button to copy the code. By - python tutorial - team
  • After executing the command, we get this error message:

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

Solution 1:

  • We need to use our package manager to install them system-wide.

For apt (Ubuntu, Debian...):

sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs 
click below button to copy the code. By - python tutorial - team

For yum (CentOS, RHEL...):

sudo yum install python-devel 
click below button to copy the code. By - python tutorial - team

For dnf (Fedora...):

sudo dnf install python2-devel  # for python2.x installs
sudo dnf install python3-devel  # for python3.x installs 
click below button to copy the code. By - python tutorial - team

Solution 2:

  • Make sure that the Python dev files come with your OS.
  • We should not hard code the library and include paths. Instead, we can use pkg-config, which will output the correct options for our specific system:
$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7 
click below button to copy the code. By - python tutorial - team
  • We may add it to your gcc line:
gcc $(pkg-config --cflags --libs python2) -Wall utilsmodule.c -o Utilc
 
click below button to copy the code. By - python tutorial - team

Related Searches to fatal error: Python.h: No such file or directory