繁体   English   中英

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

[英]fatal error: Python.h: No such file or directory

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

gcc -Wall utilsmodule.c -o Utilc

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

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

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

看起来你没有正确安装 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

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

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

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

sudo apt-get install python3-dev

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

sudo apt-get install python3.5-dev

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

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 的开发包,如果是 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 

如果您使用的是树莓派:

sudo apt-get install python3-dev

在 Fedora 上为 Python 2 运行这个:

sudo dnf install python2-devel

对于 Python 3:

sudo dnf install python3-devel

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

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

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

Cygwin的解决方案

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

您可以使用Cygwin.com32 位64 位setup.exe (取决于您的安装)快速安装它。

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

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

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

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

#include <python2.7/Python.h>

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

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

在 AWS API (centOS) 中

yum install python27-devel

AWS EC2 安装运行 python34:

sudo yum install python34-devel

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

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

情况不一样,但它也适用于我,现在我可以将SWIGPython3.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/

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

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

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

对于带有 python 3.6 的 ubuntu 16.04

sudo apt-get install python3.6-dev

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

sudo apt-get install gcc

尝试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# 

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

对于 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 一起工作

对于那里的 OpenSuse 同志:

sudo zypper install python3-devel

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

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

sudo yum install python36-devel

这是另一个解决方案,因为这些解决方案都不适合我。 作为参考,我试图在 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时,它运行良好。

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

  2. 完成后,问题是告诉编译器它们的位置以及如何针对它们进行编译。 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

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

这应该会有所帮助,特别是如果您看到/来自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 ...

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

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

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

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

有时即使在安装 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

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

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

确认如果你运行的是centos 8+。 您将需要运行以下命令:

sudo yum -y install python36 python38 python39
sudo yum -y install python36-devel.x86_64 python38-devel.x86_64 python39-devel.x86_64

pip3.8 install cpython
pip3.9 install cpython
pip3.6 install cpython

如果你使用 cmake 来构建项目,你可以使用这个例子。

cmake_minimum_required(VERSION 2.6)
project(demo)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(demo main.cpp)
target_link_libraries(demo ${PYTHON_LIBRARIES})

我遇到的情况是我的Python.h在目录/usr/include/python3.8/usr/include/python2.7 ,只要给gcc的路径like -I /usr/include/python3.8 。python的版本替换为你的。

如果您已经安装了 Python,并且想要链接到特定的 Python 版本,您可以从 Python 获取相关的包含路径。

>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/include/python3.10' # Example output

您可以使用 shell 脚本针对特定的 python 版本手动指定包含路径/构建:

#!/bin/sh
python_include=$(python3.10 -c "import sysconfig; print(sysconfig.get_path('include'))")
gcc -Wall utilsmodule.c -o Utilc -I"$python_include"

安装 python-dev python3-dev 后,仍然报告此错误。 所以我强制设置 header 路径。 在我的系统中, python 3 header 路径是 /usr/include/python3.8 所以 C_INCLUDE_PATH=/usr/include/python3.8/:/usr/include:/usr/local/include:/tmp EXPORT C_INCLUDE_PATH 编译通过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM