fatal error: Python.h: No such file or directory

2018-12-31 08:24发布

I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below:

gcc -Wall utilsmodule.c -o Utilc

After executing the command, I get this error message:

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

in fact I have tried all the suggested solutions over the internet but the problem still exists ... also I have no problem with Python.h. I managed to locate the file on my machine ... anybody has faced the same problem before??

25条回答
刘海飞了
2楼-- · 2018-12-31 09:01

In my case, what fixed it in Ubuntu was to install the packages libpython-all-dev (or libpython3-all-dev if you use Python 3).

查看更多
栀子花@的思念
3楼-- · 2018-12-31 09:03

Make sure that the Python dev files come with your OS.

You should not hard code the library and include paths. Instead, use pkg-config, which will output the correct options for your specific system:

$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7

You may add it to your gcc line:

gcc $(pkg-config --cflags --libs python2) -Wall utilsmodule.c -o Utilc
查看更多
初与友歌
4楼-- · 2018-12-31 09:03

If you use a virtualenv with a 3.6 python (edge right now), be sure to install the matching python 3.6 dev sudo apt-get install python3.6-dev, otherwise executing sudo python3-dev will install the python dev 3.3.3-1, which won't solve the issue.

查看更多
牵手、夕阳
5楼-- · 2018-12-31 09:05

If you are using a Raspberry Pi:

sudo apt-get install python-dev
查看更多
几人难应
6楼-- · 2018-12-31 09:06

Sure python-dev or libpython-all-dev are the first thing to (apt )install, but if that doesn't help as was my case, I advice you to install the foreign Function Interface packages by sudo apt-get install libffi-dev and sudo pip install cffi.

This should help out especially if you see the error as/from c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory.

查看更多
唯独是你
7楼-- · 2018-12-31 09:06

Sometimes even after installing python-dev the error persists, Check for the error if it is 'gcc' missing.

First download as stated in https://stackoverflow.com/a/21530768/8687063, then install gcc

For apt (Ubuntu, Debian...):

sudo apt-get install gcc

For yum (CentOS, RHEL...):

sudo yum install gcc

For dnf (Fedora...):

sudo dnf install gcc

For zypper (openSUSE...):

sudo zypper in gcc

For apk (Alpine...):

sudo apk gcc
查看更多
登录 后发表回答