致命错误:Python.h:没有此文件或目录
- 2024-11-25 08:49:00
- admin 原创
- 208
问题描述:
我正在尝试使用 C 扩展文件构建共享库,但首先我必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc
执行该命令后,我收到以下错误消息:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
我尝试了互联网上建议的所有解决方案,但问题仍然存在。我没有遇到任何问题Python.h
。我设法在我的计算机上找到了该文件。
解决方案 1:
看起来您尚未正确安装 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
重要提示: python3-dev/devel 不会自动覆盖 python3 的所有次要版本。
例如,如果您使用的是 python 3.11,则可能需要安装python3.11-dev
/python3.11-devel
。
解决方案 2:
在 Ubuntu 上,我运行的是 Python 3,因此必须安装
sudo apt-get install python3-dev
如果要使用未链接到 python3 的 Python 版本,请安装关联的 python3.x-dev 包。例如:
sudo apt-get install python3.5-dev
解决方案 3:
对于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
sudo apt install libpython3.11-dev
sudo apt install libpython3.12-dev
解决方案 4:
你必须要做两件事。
安装 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
解决方案 5:
在 Fedora 上针对 Python 2 运行此命令:
sudo dnf install python2-devel
对于 Python 3:
sudo dnf install python3-devel
解决方案 6:
如果您使用tox在多个版本的 Python 上运行测试,则可能需要为您正在测试的每个版本的 Python 安装 Python dev 库。
sudo apt-get install python2.6-dev
sudo apt-get install python2.7-dev
etc.
解决方案 7:
确保 Python dev 文件随你的操作系统一起提供。
您不应该对库和包含路径进行硬编码。相反,请使用 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)
解决方案 8:
对我来说,将其更改为以下内容有效:
#include <python2.7/Python.h>
我找到了文件/usr/include/python2.7/Python.h
,并且由于/usr/include
已经在包含路径中,所以python2.7/Python.h
应该足够了。
您也可以从命令行添加包含路径 - gcc -I/usr/lib/python2.7
(感谢@erm3nda)。
解决方案 9:
Cygwin的解决方案
您需要安装包python2-devel
或python3-devel
,具体取决于您使用的 Python 版本。
您可以使用Cygwin.com上的32 位或64 位 setup.exe
(取决于您的安装)快速安装它。
示例(setup.exe
如果需要,请修改文件名和 Python 的主版本):
$ setup.exe -q --packages=python3-devel
您还可以查看我的其他答案,了解从命令行安装 Cygwin 软件包的更多选项。
解决方案 10:
在 AWS API(centOS)中
yum install python27-devel
解决方案 11:
AWS EC2 安装运行 python34:
sudo yum install python34-devel
解决方案 12:
当您安装了不同的 Python 版本并且使用非系统 pip 时,也会出现此问题。在这种情况下,非系统 pip 将找不到正确版本的 Python 头文件。
当我尝试使用pip 安装与应用程序捆绑在一起的 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>
解决方案 13:
如果您使用带有 3.6 python(目前为 edge)的虚拟环境,请确保安装匹配的 python 3.6 dev sudo apt-get install python3.6-dev
,否则执行sudo python3-dev
将安装 python dev 3.3.3-1,这不会解决问题。
解决方案 14:
我设法解决了这个问题,并在一个命令中生成了.so 文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c
解决方案 15:
就我而言,在 Ubuntu 中修复该问题的方法是安装软件包libpython-all-dev
(或者libpython3-all-dev
如果你使用 Python 3)。
解决方案 16:
这不是相同的情况,但它对我来说也有效,现在我可以将SWIG与Python3.5一起使用:
我试图编译:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
使用 Python 2.7 可以正常工作,但使用我的版本 3.5 则不行:
exite_wrap.c:147:21:致命错误:Python.h:不存在存档或目录编译终止。
在我的 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/
解决方案 17:
我在ubuntu中安装coolprop的时候也遇到了这个错误。
对于使用 Python 3.6 的 ubuntu 16.04
sudo apt-get install python3.6-dev
如果这不起作用,请尝试安装/更新gcc
lib。
sudo apt-get install gcc
解决方案 18:
尝试 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#
现在您可以像专家一样猜测一下该选择哪一个。
解决方案 19:
这是另一个解决方案,因为这些解决方案对我都不起作用。作为参考,我尝试pip install
在 Amazon Linux AMI 基础 Docker 映像上为 Python 3.6 执行某些操作。
非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
Docker解决方案:
# 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
,它运行正常。
解决方案 20:
对于 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 一起工作
解决方案 21:
对于 OpenSuse 的同志们:
sudo zypper install python3-devel
解决方案 22:
如果您在 Amazon Linux 上使用 Python 3.6(基于 RHEL,但此处给出的 RHEL 答案不起作用):
sudo yum install python36-devel
解决方案 23:
如果您的操作系统没有随附 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
。
解决方案 24:
当然python-dev
,或者libpython-all-dev
是第一件事(apt
)install
,但如果这没有帮助,就像我的情况一样,我建议您通过和安装外部函数接口包。sudo apt-get install libffi-dev
`sudo pip install cffi`
这应该会有所帮助,特别是当您看到错误为/来自时c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory
。
解决方案 25:
尝试找到你的 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 ...
解决方案 26:
我在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=)
解决方案 27:
当我尝试在 CentOS 7 上使用 Python3.6 安装 ctds 时,出现了此错误。我使用了这里提到的所有技巧,包括yum install python34-devel
。问题出现Python.h
在/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
在链接 ctds 时找不到。
最后,有效的方法是修复 Python3.6 的开发环境,需要使用 include 和 libs 进行更正。
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
通常应该位于其中。
解决方案 28:
有时即使安装了 python-dev 后错误仍然存在,请检查错误是否是缺少“gcc”。
首先按照https://stackoverflow.com/a/21530768/8687063中所述下载,然后安装 gcc
对于 apt(Ubuntu、Debian...):
sudo apt-get install gcc
对于 yum (CentOS、RHEL...):
sudo yum install gcc
对于 dnf (Fedora...):
sudo dnf install gcc
对于 zypper (openSUSE...):
sudo zypper in gcc
对于 apk (Alpine...):
sudo apk gcc
解决方案 29:
当您尝试删除python3.5
和安装时它经常出现python3.6
。
所以当使用python3
(which python3 -V
=> python3.6
) 安装一些包所需的python3.5
标头时就会出现这个错误。
通过安装模块解决python3.6-dev
。
解决方案 30:
这意味着它Python.h
不在您的编译器默认包含路径中。您是在系统范围内还是在本地安装的?您的操作系统是什么?
您可以使用-I<path>
标志来指定编译器应在其中查找标头的附加目录。您可能必须跟进,-L<path>
以便 gcc 可以使用找到要链接的库-l<name>
。