ChatGPT解决这个技术问题 Extra ChatGPT

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

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

gcc -Wall utilsmodule.c -o Utilc

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

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

我已经通过互联网尝试了所有建议的解决方案,但问题仍然存在。我对 Python.h 没有任何问题。我设法在我的机器上找到该文件。


w
wim

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

对于 aptUbuntu、Debian...):

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

对于 yumCentOS、RHEL...):

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

对于 dnfFedora...):

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

对于 zypperopenSUSE...):

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

对于 apkAlpine...):

# 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-cygCygwin...):

apt-cyg install python-devel   # for python2.x installs
apt-cyg install python3-devel  # for python3.x installs

注意:python3-dev 不会自动覆盖 python3 的所有次要版本,如果您使用例如 python 3.8,您可能需要安装 python3.8-dev。


Arch Linux 呢?
如果我们在机器上没有 su 访问权限怎么办?我正在使用本地用户授权的虚拟环境中工作。是否有任何类似“pip”的修复程序可用,或者我们可以在某个地方手动放置 Python.h 的副本?
F
FreshPow

在 Ubuntu 上,我运行的是 Python 3,我必须安装

sudo apt-get install python3-dev

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

sudo apt-get install python3.5-dev

如果您使用具有不同 python 版本的虚拟环境,请确保在安装必要的开发包后创建虚拟环境。否则头文件将无法正确复制。
i
ijoseph

特别是对于 Python 3.7 和 Ubuntu,我需要

sudo apt install libpython3.7-dev

.我认为在某些时候名称已从 pythonm.n-dev 更改为此。

对于 Python 3.6、3.8 到 3.10(以及计数……)类似:

sudo apt install libpython3.6-dev

sudo apt install libpython3.8-dev

sudo apt install libpython3.9-dev

sudo apt install libpython3.10-dev


与 Python 3.10 相同
仍然相关并且有效。
很烦人。非常感谢!
v
vartec

你必须做两件事。

安装 Python 的开发包,如果是 Debian/Ubuntu/Mint,它是通过命令完成的:

sudo apt-get install python-dev

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

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

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

gcc -Wall -I/usr/include/python2.7 -lpython2.7  utilsmodule.c -o Utilc 

我是否必须为 C 扩展文件中的每个包含文件添加标志?
如果您使用的是其他版本的 Python,例如 3.3:sudo apt-get install python3.3-dev
M
Martin Tournoij

在 Fedora 上为 Python 2 运行这个:

sudo dnf install python2-devel

对于 Python 3:

sudo dnf install python3-devel

在 Mint 18.2(基于 Ubuntu)上,它是 apt-get install python-dev
C
Christian Long

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

sudo apt-get install python2.6-dev 
sudo apt-get install python2.7-dev 
etc.

s
sleblanc

确保 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) 

我在 RHEL 上收到以下错误消息:“未知选项 -I/usr/include/python2.7”
@FedorSteeman,有人编辑了我的答案并从命令中删除了换行符。
D
David Ferenczy Rogožan

Cygwin 的解决方案

您需要安装软件包 python2-develpython3-devel,具体取决于您使用的 Python 版本。

您可以使用 Cygwin.com 中的 32-bit64-bit setup.exe(取决于您的安装)快速安装它。

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

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

您还可以查看我的 other answer,了解更多从命令行安装 Cygwin 软件包的选项。


s
sashoalm

对我来说,将其更改为此有效:

#include <python2.7/Python.h>

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

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


所有其他答案都告诉你安装一些东西。这个对我有用。为什么这不是最佳答案?
@ uoɥʇʎPʎzɐɹC 也许是因为它不会在 python3 下运行?
最好将 lib 设置为 gcc -I/usr/lib/python2.7 etc 而不是硬编码包含调用。
@noɥʇʎԀʎzɐɹƆ 因为它应该可以在不更改代码的情况下工作。您经常需要编译不属于您的代码,例如某些外部依赖项,而您能做的最糟糕的事情就是修改该代码。
@DawidFerenczy 了解。
T
Tadeusz Kopec for Ukraine

在 AWS API (centOS) 中

yum install python27-devel

这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时对自己的帖子发表评论,一旦您有足够的reputation,您就可以comment on any post
@Wtower 是的,它确实提供了答案。
它确实提供了答案。尽管您必须将版本替换为您需要的版本。
这有助于克服我在亚马逊 Linux 实例上尝试 pip install cryptography 时遇到的问题。
这是一个有用的答案,虽然可以在 Amazon Linux 上安装 python-devel 或 python2-devel,但在 virtualenv 中运行 pip install 时,这是唯一对我有用的
p
parsethis

AWS EC2 安装运行 python34:

sudo yum install python34-devel


G
Guillaume Cisco

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


在 3.5(.2) 上也对我有用。为您的 Python 版本显式安装正确的开发包是一件好事。
O
Oriol Nieto

就我而言,在 Ubuntu 中修复它的方法是安装软件包 libpython-all-dev(或 libpython3-all-dev,如果您使用 Python 3)。


python-all-dev 在我的情况下,但足够接近。
@Oriol Nieto,非常感谢。 python-all-dev 也为我解决了这个问题。
D
David Ferenczy Rogožan

情况不一样,但它也适用于我,现在我可以将 SWIG 与 Python3.5 一起使用:

我试图编译:

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

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

exists_wrap.c:147:21: 致命错误: Python.h: No 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/

S
Solomon Ucko

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

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

W
Wreeecks

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

对于带有 python 3.6 的 ubuntu 16.04

sudo apt-get install python3.6-dev

如果这不起作用,请尝试安装/更新 gcc lib。

sudo apt-get install gcc

天哪,我为此苦苦挣扎了很长时间,因为我运行了 python3-dev 很多次并不断收到相同的错误,但不知道 python3.6-dev!谢谢!!
A
Aleksandr M

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

例如:

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# 

现在,您可以做出专家猜测,选择哪一个。


P
Paul Roub

对于 CentOS 7:

sudo yum install python36u-devel

我按照此处的说明在多个 VM 上安装 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 一起使用


k
kmonsoor

对于那里的 OpenSuse 同志:

sudo zypper install python3-devel

M
Milo

当您安装了不同的 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 <package>

-I 的目的是什么?
@SultanAhmedSagor -I 标志告诉编译器它是查找头文件的路径(我猜是 -I 作为“包含”):caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html
谢谢,这给了我需要的提示-我需要安装 python3.8-dev :)
我把这个和 "rpm -ql python36-devel.x86_64 | grep -i "Python.h" 解决方案混合在一起。帮助很大!谢谢!
Y
Yigal

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

sudo yum install python36-devel

感谢您的评论,它拯救了一天!
W
William

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

非码头解决方案:

# 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 映像。例如,我试图在 python:3.9.4-slim 上安装 asyncpg~=0.24.0,这会产生与您看到的相同的错误。但是,当我将图像更新为 python:3 时,它运行良好。


我的问题的确切解决方案,谢谢!
像魅力一样为亚马逊 linux 工作。很高兴我找到了你的解决方案。谢谢
截至 2022 年进展顺利。我在构建规范的一行中添加了您指出的命令,用 ; 分隔所以 EXPORT 会起作用
R
Rahul Jha

如果您的操作系统没有附带 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-configpython3.6-config


H
Huge

当然 python-devlibpython-all-dev 是 (apt )install 的第一件事,但如果这对我的情况没有帮助,我建议您安装 foreign Function Interface sudo apt-get install libffi-devsudo pip install cffi 打包。

这应该会有所帮助,尤其是当您看到来自 c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory 的错误时。


sudo pip3 install cffi (# for python3)
g
gemfield

尝试找到您的 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 ...

整洁的。谢谢!
V
Valery Noname

我在 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=)

注意:python3-dev 不会自动覆盖 python3 的所有次要版本,如果您使用的是例如 python 3.8,您可能需要安装 python3.8-dev
问题是我有一个错误的包含 #include "Python.h"。编译器看不到它。您应该手动编辑路径。或者您可以使用我发布的 Makefile。此文件与 #include 的手动编辑相同
B
Babu Arunachalam

当我尝试使用 Python3.6 在 CentOS 7 上安装 ctds 时出现此错误。我做了这里提到的所有技巧,包括 yum install python34-devel。问题是在 /usr/include/python3.4m but not in /usr/include/python3.6m 中发现了 Python.h。我尝试使用 --global-option 指向包含目录 (pip3.6 install --global-option=build_ext --global-option="--include-dirs=/usr/include/python3.4m" ctds)。这导致在链接 ctds 时找不到 lpython3.6m

最后起作用的是修复 Python3.6 的开发环境需要使用包含和库进行更正。

yum -y install https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/python36u-libs-3.6.3-1.ius.centos7.x86_64.rpm

Python.h 需要在 gcc 的包含路径中。无论使用哪个版本的 python,例如,如果它是 3.6,那么它通常应该在 /usr/include/python3.6m/Python.h 中。


不知道为什么会有 -1,但对于带有 CentOS 的 python3.6,这是一个问题,因为 python36-devel 不可用。您需要从上面的 URL 安装它。
d
dphans

当您尝试删除 python3.5 并安装 python3.6 时,它经常出现。

所以在使用python3(其中python3 -V => python3.6)安装一些需要的包时需要python3.5头会出现这个错误。

通过安装 python3.6-dev 模块解决。


H
HimanshuGahlot

有时即使在安装 python-dev 之后错误仍然存在,如果缺少“gcc”,请检查错误。

首先按照https://stackoverflow.com/a/21530768/8687063中的说明下载,然后安装 gcc

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

sudo apt-get install gcc

对于百胜(CentOS,RHEL ...):

sudo yum install gcc

对于 dnf(Fedora ...):

sudo dnf install gcc

对于 zypper (openSUSE...):

sudo zypper in gcc

对于 apk(高山...):

sudo apk gcc

K
Kos

这意味着 Python.h 不在编译器的默认包含路径中。您是在系统范围内还是在本地安装它?你的操作系统是什么?

您可以使用 -I<path> 标志指定编译器应在其中查找标头的附加目录。您可能必须跟进 -L<path>,以便 gcc 可以找到您将使用 -l<name> 链接的库。