致命错误:Python.h:没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21530577/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fatal error: Python.h: No such file or directory
提问by Mohanad Y.
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:
我正在尝试使用 C 扩展文件构建一个共享库,但首先我必须使用以下命令生成输出文件:
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.
utilsmodule.c:1:20: 致命错误: Python.h: 没有这样的文件或目录编译终止。
I have tried all the suggested solutions over the internet but the problem still exists. I have no problem with Python.h. I managed to locate the file on my machine.
我已经通过互联网尝试了所有建议的解决方案,但问题仍然存在。我没有问题Python.h。我设法在我的机器上找到了该文件。
采纳答案by Mohanad Y.
I managed to solve this issue and generate the .so file in one command
我设法解决了这个问题并在一个命令中生成了 .so 文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7  utilsmodule.c
回答by wim
Looks like you haven't properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide.
看起来你没有正确安装 python dev 的头文件和静态库。使用您的包管理器在系统范围内安装它们。
For apt(Ubuntu, Debian...):
对于apt(Ubuntu、Debian...):
sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs
For yum(CentOS, RHEL...):
对于yum(CentOS、RHEL...):
sudo yum install python-devel   # for python2.x installs
sudo yum install python3-devel   # for python3.x installs
For dnf(Fedora...):
对于dnf(Fedora...):
sudo dnf install python2-devel  # for python2.x installs
sudo dnf install python3-devel  # for python3.x installs
For zypper(openSUSE...):
对于zypper(openSUSE...):
sudo zypper in python-devel   # for python2.x installs
sudo zypper in python3-devel  # for python3.x installs
For apk(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
For apt-cyg(Cygwin...):
对于apt-cyg(Cygwin ...):
apt-cyg install python-devel   # for python2.x installs
apt-cyg install python3-devel  # for python3.x installs
回答by Kos
This means that Python.hisn't in your compiler's default include paths. Have you installed it system-wide or locally? What's your OS?
这意味着它Python.h不在编译器的默认包含路径中。您是在系统范围内还是在本地安装了它?你的操作系统是什么?
You could use the -I<path>flag to specify an additional directory where your compiler should look for headers. You will probably have to follow up with -L<path>so that gcc can find the library you'll be linking with using -l<name>.
您可以使用该-I<path>标志来指定一个额外的目录,您的编译器应该在其中查找头文件。您可能需要跟进,-L<path>以便 gcc 可以找到您将使用-l<name>.
回答by vartec
Two things you have to do.
你必须做两件事。
Install development package for Python, in case of Debian/Ubuntu/Mint it's done with command:
为 Python 安装开发包,在 Debian/Ubuntu/Mint 的情况下,它使用命令完成:
sudo apt-get install python-dev
Second thing is that include files are not by default in the include path, nor is Python library linked with executable by default. You need to add these flags (replace Python's version accordingly):
第二件事是包含文件默认不在包含路径中,默认情况下 Python 库也不与可执行文件链接。您需要添加这些标志(相应地替换 Python 的版本):
-I/usr/include/python2.7 -lpython2.7 
In other words your compile command ought to be:
换句话说,您的编译命令应该是:
gcc -Wall -I/usr/include/python2.7 -lpython2.7  utilsmodule.c -o Utilc 
回答by sleblanc
Make sure that the Python dev files come with your OS.
确保 Python 开发文件随您的操作系统一起提供。
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,它将为您的特定系统输出正确的选项:
$ pkg-config --cflags --libs python2 -I/usr/include/python2.7 -lpython2.7
You may add it to your gccline:
您可以将它添加到您的gcc行:
gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2) 
回答by FreshPow
On Ubuntu, I was running Python 3 and I had to install
在 Ubuntu 上,我运行的是 Python 3,我必须安装
sudo apt-get install python3-dev
If you want to use a version of Python that is not linked to python3, install the associated python3.x-dev package. For example:
如果要使用未链接到 python3 的 Python 版本,请安装关联的 python3.x-dev 包。例如:
sudo apt-get install python3.5-dev
回答by kmonsoor
For the OpenSuse comrades out there:
对于那里的 OpenSuse 同志:
sudo zypper install python3-devel
回答by Christian Long
回答by ravi.zombie
on Fedora run this for Python 2:
在 Fedora 上为 Python 2 运行这个:
sudo dnf install python2-devel
and for Python 3:
对于 Python 3:
sudo dnf install python3-devel
回答by yespbs
In AWS API (centOS) its
在 AWS API (centOS) 中
yum install python27-devel

