致命错误:Python.h:没有这样的文件或目录 您所在的位置:网站首页 python34版本 致命错误:Python.h:没有这样的文件或目录

致命错误:Python.h:没有这样的文件或目录

#致命错误:Python.h:没有这样的文件或目录| 来源: 网络整理| 查看: 265

我正在尝试使用C扩展文件构建共享库,但首先我必须使用以下命令生成输出文件:

gcc -Wall utilsmodule.c -o Utilc

执行命令后,我收到此错误消息:

utilsmodule.c:1:20:致命错误:Python.h:没有这样的文件或目录编译终止.

事实上我已经通过互联网尝试了所有建议的解决方案,但问题仍然存在...我也没有问题Python.h.我设法在我的机器上找到该文件......任何人在遇到同样的问题之前?

问题回答

看起来你没有正确安装python dev的头文件和静态库.使用包管理器在系统范围内安装它们.

对于apt(Ubuntu,Debian ......):

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

对于yum(CentOS,RHEL ......):

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

对于dnf(Fedora ......):

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

对于zypper(openSUSE ...):

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

对于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

对于apt-cyg(Cygwin ......):

apt-cyg install python-devel # for python2.x installs apt-cyg install python3-devel # for python3.x installs 问题回答

在Ubuntu上,我运行的是Python 3,我不得不安装

sudo apt-get install python3-dev

如果要使用未链接到python3的Python版本,请安装相关的python3.x-dev软件包.例如:

sudo apt-get install python3.5-dev 问题回答

你需要做的两件事.

安装Python的开发包,如果是Debian/Ubuntu/Mint,则使用命令完成:

sudo apt-get install python-dev

第二件事是包含文件默认情况下不包含在include路径中,默认情况下Python库也不与可执行文件链接.您需要添加这些标志(相应地替换Python的版本):

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

换句话说,你的编译命令应该是:

gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc 问题回答

如果您使用的是Raspberry Pi:

sudo apt-get install python-dev 问题回答

在Fedora上为Python 2运行这个:

sudo dnf install python2-devel

对于Python 3:

sudo dnf install python3-devel 问题回答

如果您使用tox在多个版本的Python上运行测试,则可能需要为要测试的每个Python版本安装Python开发库.

sudo apt-get install python2.6-dev sudo apt-get install python2.7-dev etc. 问题回答Cygwin的解决方案

您需要安装软件包,python2-devel或者python3-devel根据您使用的Python版本进行安装.

您可以使用Cygwin.com上的32位或64位setup.exe(取决于您的安装)快速安装它.

示例(setup.exe如果需要,修改文件名和Python的主要版本):

$ setup.exe -q --packages=python3-devel

您还可以查看我的其他答案以获取更多选项,以便从命令行安装Cygwin的软件包.

问题回答

在AWS API(centOS)中

yum install python27-devel 问题回答

For Python 3.7and Ubuntu in particular, I needed

sudo apt install libpython3.7-dev

. I think at some point names were changed from pythonm.n-devto this.

问题回答

对我来说,改变它是有效的:

#include

我找到了该文件/usr/include/python2.7/Python.h,并且因为/usr/include已经在include路径中,那么python2.7/Python.h应该就足够了.

您也可以从命令行添加包含路径 - gcc -I/usr/lib/python2.7(感谢@ erm3nda).

问题回答

确保Python开发文件随操作系统一起提供.

您不应该对库进行硬编码并包含路径.相反,使用pkg-config,它将为您的特定系统输出正确的选项:

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

您可以将它添加到您的gcc行:

gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2) 问题回答

运行python34的AWS EC2安装:

sudo yum install python34-devel

问题回答

在我的例子中,在Ubuntu中修复它的是安装包libpython-all-dev(或者libpython3-all-dev如果你使用Python 3).

问题回答

这不是同样的情况,但它也适用于我,现在我可以使用SWIG与Python3.5:

我试图编译:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

使用Python 2.7工作正常,而不是我的版本3.5:

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

现在我可以编译没有问题Python3.5:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/ 问题回答

如果你使用带有3.6 python的virtualenv(现在是edge),请确保安装匹配的python 3.6 dev sudo apt-get install python3.6-dev,否则执行sudo python3-dev将安装python dev 3.3.3-1,这将无法解决问题.

问题回答

我在ubuntu中安装coolprop时遇到了这个错误.

对于带有python 3.6的ubuntu 16.04

sudo apt-get install python3.6-dev

如果这不起作用,请尝试安装/更新gcclib.

sudo apt-get install gcc 问题回答

我设法解决了这个问题,并在一个命令中生成.so文件

gcc -shared -o UtilcS.so -fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c 问题回答

尝试apt-file.很难记住丢失文件所在的包名称.它是通用的,对任何包文件都很有用.

例如:

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 问题回答

对于CentOS 7:

sudo yum install python36u-devel

我按照这里的说明在几个虚拟机上安装python3.6:https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming- environment-on-centos-7然后能够构建mod_wsgi并使用python3.6 virtualenv

问题回答

如果您在 Amazon Linux 上使用 Python 3.6(基于 RHEL,但此处给出的 RHEL 答案不起作用):

sudo yum install python36-devel 问题回答

这是另一个解决方案,因为这些解决方案都不适合我。作为参考,我尝试pip install在 Python 3.6 的 Amazon Linux AMI 基础 Docker 映像上做一些事情。

非码头解决方案:

# Install python3-devel like everyone says yum -y install python36-devel.x86_64 # Find the install directory of `Python.h` rpm -ql python36-devel.x86_64 | grep -i "Python.h" # Forcefully add it to your include path C_INCLUDE_PATH='/usr/include/python3.6m' export C_INCLUDE_PATH

码头工人解决方案:

# Install python3-devel like everyone says RUN yum -y install python36-devel.x86_64 # Find the install directory of `Python.h`, for me it was /usr/include/python3.6m RUN rpm -ql python36-devel.x86_64 | grep -i "Python.h" && fake_command_so_docker_fails_and_shows_us_the_output # Since the previous command contains a purposeful error, remove it before the next run # Forcefully add it to your include path ARG C_INCLUDE_PATH='/usr/include/python3.6m'

注意:如果您在编译 C++ 时遇到错误,请使用CPLUS_INCLUDE_PATH.

或者,您可能更喜欢使用另一个 Docker 映像。例如,我试图在 上安装asyncpg~=0.24.0,python:3.9.4-slim这产生了与您看到的相同的错误。但是,当我将图像更新为 时python:3,它工作正常。

问题回答

如果您的操作系统提供的 Python 没有附带,您必须在您的操作系统上安装 Python 开发文件。这个问题的许多答案显示了可以在不同系统上实现的无数方法。

当你这样做时,问题是告诉编译器它们所在的位置以及如何针对它们进行编译。Python 附带了一个名为python-config. 对于编译,您需要--includes输出,并且为了将程序链接到 Python 库(将 Python 嵌入到您的程序中)需要--ldflags输出。例子:

gcc -c mypythonprogram.c $(python3-config --includes) gcc -o program mypythonprogram.o $(python3-config --ldflags)

该python-config程序可以以 Python 版本命名 - 例如在 Debian、Ubuntu 上,这些可以命名为python3-config或python3.6-config.

问题回答

当然python-dev或者libpython-all-dev是 ( apt)的第一件事install,但如果这对我的情况没有帮助,我建议您通过和安装外部函数接口包。sudo apt-get install libffi-devsudo pip install cffi

这应该会有所帮助,特别是如果您看到错误 as/from c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory。

问题回答

尝试找到您的 Python.h:

gemfield@ThinkPad-X1C:~$ locate Python.h /home/gemfield/anaconda3/include/python3.7m/Python.h /home/gemfield/anaconda3/pkgs/python-3.7.6-h0371630_2/include/python3.7m/Python.h /usr/include/python3.8/Python.h

如果找不到,则安装 python-dev 或 python3-dev;否则包括编译器的正确头文件路径:

g++ -I/usr/include/python3.8 ... 问题回答

当您安装了不同的 Python 版本并且您使用的 pip 不是系统的 pip 时,也会出现此问题。在这种情况下,非系统 pip 将找不到正确版本的 Python 标头。

当我尝试为与应用程序捆绑的 Python安装包时,它发生在我身上。因为它不是系统的python,所以apt install pythonXX-dev没有用。

在这种情况下,解决方案是找到正确的 python 标头:

find / -iname 'Python.h'

在输出中,您将看到系统 python 标头,并且希望是您正在寻找的标头,例如:

/usr/include/python3.7m/Python.h /usr/include/python3.6m/Python.h /home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h /home/ubuntu/miniconda3/pkgs/python-3.8.5-h7579374_1/include/python3.8/Python.h /home/ubuntu/miniconda3/pkgs/python-3.7.0-h6e4f718_3/include/python3.7m/Python.h /home/ubuntu/miniconda3/include/python3.8/Python.h /home/ubuntu/miniconda3/envs/sim/include/python3.7m/Python.h /home/ubuntu/src/blender-deps/Python-3.7.7/Include/Python.h /opt/lib/python-3.7.7/include/python3.7m/Python.h

然后,您可以设置一个编译器标志,当被 pip 调用时,该标志将被 gcc 使用。我的是/home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h,所以我做了:

export CPPFLAGS=-I/home/ubuntu/src/blender-deps/Python-3.7.7/Include pip install 问题回答

我在Ubuntu上。我已经按照一些答案中的建议安装了所有软件包。

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

我仍然有这个问题,这条线:

#include "Python.h"

还有一些,我可以手动编辑它们,这是一种不好的做法。我现在知道了这个秘密,它来自cython源代码。我有文件。它编译没有错误。那是文件。将 PYTHON 更改为您拥有的 python 版本,python/python3。将 FILE 更改为您的 c 文件名。makefile 文件的名称应该是Makefile。使用以下命令运行文件:

make all 用于创建我们的独立 Cython 程序的 Makefile FILE := file.c PYTHON := python3 PYVERSION := $(shell $(PYTHON) -c "import sys; print(sys.version[:3])") PYPREFIX := $(shell $(PYTHON) -c "import sys; print(sys.prefix)") INCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_python_inc())") PLATINCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_python_inc(plat_specific=True))") LIBDIR1 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") LIBDIR2 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBPL'))") PYLIB := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBRARY')[3:-2])") CC := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('CC'))") LINKCC := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LINKCC'))") LINKFORSHARED := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LINKFORSHARED'))") LIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LIBS'))") SYSLIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('SYSLIBS'))") .PHONY: paths all clean test paths: @echo "PYTHON=$(PYTHON)" @echo "PYVERSION=$(PYVERSION)" @echo "PYPREFIX=$(PYPREFIX)" @echo "INCDIR=$(INCDIR)" @echo "PLATINCDIR=$(PLATINCDIR)" @echo "LIBDIR1=$(LIBDIR1)" @echo "LIBDIR2=$(LIBDIR2)" @echo "PYLIB=$(PYLIB)" @echo "CC=$(CC)" @echo "LINKCC=$(LINKCC)" @echo "LINKFORSHARED=$(LINKFORSHARED)" @echo "LIBS=$(LIBS)" @echo "SYSLIBS=$(SYSLIBS)" $(FILE:.c=): $(FILE:.c=.o) $(LINKCC) -o $@ $^ -L$(LIBDIR1) -L$(LIBDIR2) -l$(PYLIB) $(LIBS) $(SYSLIBS) $(LINKFORSHARED) $(FILE:.c=.o): $(FILE) $(CC) -c $^ -I$(INCDIR) -I$(PLATINCDIR) all: $(FILE:.c=) 本文标题: 致命错误:Python.h:没有这样的文件或目录 本文网址: https://www.aigwa.com/cms/Content/detail/4058.html 本文标签: python-c-extensionpython-c-apigccpython


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有