致命错误:Python.h:没有这样的文件或目录
我正在尝试使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:致命错误:Python.h:没有这样的文件或目录,python,gcc,python-c-api,Python,Gcc,Python C Api,我正在尝试使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件: gcc-Wall utilsmodule.c-o Utilc 执行命令后,我收到以下错误消息: > utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated. 我在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对Python.h没有问题。我设法在我的计算机上找到了该文件。看起来您没
gcc-Wall utilsmodule.c-o Utilc
执行命令后,我收到以下错误消息:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
我在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对
Python.h
没有问题。我设法在我的计算机上找到了该文件。看起来您没有正确安装python开发的头文件和静态库。请使用软件包管理器在系统范围内安装它们
对于apt(Ubuntu,Debian…):
对于yum
(CentOS,RHEL…):
对于dnf
(Fedora…):
对于zypper
(openSUSE…):
对于apk
(Alpine…):
对于apt cyg
(Cygwin…):
这意味着Python.h
不在编译器的默认包含路径中。您是否在系统范围内或本地安装了it?你的操作系统是什么
您可以使用-I
标志指定编译器应该在其中查找头的附加目录。您可能需要继续使用-L
,以便gcc可以找到您将使用-L
链接的库。您必须做两件事
安装Python开发包,对于Debian/Ubuntu/Mint,使用以下命令完成:
sudo apt-get install python-dev
第二件事是,默认情况下,include文件不在include路径中,Python库也不与可执行文件链接。您需要添加以下标志(相应地替换Python版本):
换句话说,编译命令应该是:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc
确保操作系统附带Python开发文件
您不应该硬编码库并包含路径。相反,请使用pkg config,它将为您的特定系统输出正确的选项:
$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7
您可以将其添加到gcc行:
我设法解决了这个问题,并在一个命令中生成了.so文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c
在Ubuntu上,我运行的是Python3,我必须安装它
sudo apt-get install python3-dev
如果要使用未链接到python3的Python版本,请安装相关的python3.x-dev包。例如:
sudo apt-get install python3.5-dev
root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root@ubuntu234:~/auto#
对于OpenSuse的同志们:
sudo zypper install python3-devel
如果使用在多个版本的Python上运行测试,则可能需要为正在测试的每个版本的Python安装Python开发库
sudo apt-get install python2.6-dev
sudo apt-get install python2.7-dev
etc.
在Fedora上,为Python 2运行以下命令:
sudo dnf install python2-devel
对于Python 3:
sudo dnf install python3-devel
在AWS API(centOS)中
如果您使用的是覆盆子Pi:
sudo apt-get install python3-dev
试试apt文件。很难记住丢失文件所在的包名。它是通用的,对任何包文件都很有用
例如:
sudo apt-get install python3.5-dev
root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root@ubuntu234:~/auto#
现在,您可以根据专家的猜测从中选择一个。对于我来说,将其更改为以下选项:
#include <python2.7/Python.h>
#包括
我发现文件/usr/include/python2.7/python2.h
,既然/usr/include
已经在include路径中,那么python2.7/Python.h
就足够了
您还可以从命令行添加include路径,而不是-gcc-I/usr/lib/python2.7
(谢谢@erm3nda)。在我的情况下,Ubuntu中的修复方法是安装包libpython all dev
(或者libpython 3 all dev
,如果您使用Python 3).当然python-dev
或libpython-all-dev
是(apt
)安装的第一件事,但是如果这对我的情况没有帮助,我建议您通过sudo-apt-get-install-libffi-dev
和sudo-pip-install-cffi>安装外部函数接口包
这应该会有帮助,特别是当您看到错误为/fromc/\u cffi\u backend.c:2:20:致命错误:Python.h:没有这样的文件或目录
AWS EC2安装运行python34:
sudo-yum安装python34-devel
Cygwin解决方案
您需要安装软件包python2-devel
或python3-devel
,具体取决于您使用的Python版本
您可以从中使用或setup.exe
(取决于您的安装)快速安装它
示例(如果需要,请修改setup.exe的文件名和Python的主要版本):
您还可以通过命令行查看my以获得更多选项来安装Cygwin的软件包。如果您使用带有3.6 python的virtualenv(现在使用edge),请确保安装匹配的python 3.6 devsudo apt get install python3.6-dev
,否则执行sudo python3 dev
将安装python dev 3.3.3-1,这并不能解决问题。情况不一样,但它对我也有效,现在我可以将SWIG与Python3.5一起使用:
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
sudo yum install python-devel # for python2.x installs
sudo yum install python3-devel # for python3.x installs
sudo dnf install python2-devel # for python2.x installs
sudo dnf install python3-devel # for python3.x installs
sudo zypper in python-devel # for python2.x installs
sudo zypper in python3-devel # for python3.x installs
# 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
apt-cyg install python-devel # for python2.x installs
apt-cyg install python3-devel # for python3.x installs
我试图编译:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
使用Python 2.7可以很好地工作,而不是我的3.5版:
sudo apt-get install python3-dev # for python3.x installs
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
sudo apt-get install gcc
sudo yum install gcc
sudo dnf install gcc
sudo zypper in gcc
sudo apk gcc
existe_wrap.c:147:21:致命错误:Python.h:没有existe el archivo o el
directorio编译已终止
在我的Ubuntu 16.04安装中运行后:
sudo apt-get install python3-dev # for python3.x installs
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
sudo apt-get install gcc
sudo yum install gcc
sudo dnf install gcc
sudo zypper in gcc
sudo apk gcc
现在我可以毫无问题地编译Python3.5:
sudo apt-get install python3-dev # for python3.x installs
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
sudo apt-get install gcc
sudo yum install gcc
sudo dnf install gcc
sudo zypper in gcc
sudo apk gcc
当我试图用Python3.6在CentOS 7上安装CTD时,发生了此错误。我完成了这里提到的所有技巧,包括yum安装python34-devel
。问题是Python.h
在/usr/include/python3.4m中找到,但在/usr/include/python3.6m中找不到。我试图使用--global option
指向include dir(pip3.6安装--global option=build\u ext--global option=“--include dirs=/usr/include/python3.4m”ctds
)。这导致链接CTD时未找到lpython3.6m
最后,解决Python3.6的开发环境需要使用