我试图使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc
执行命令后,我收到以下错误消息:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
我已经在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对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没有问题。我设法在我的机器上找到了该文件。
看起来您没有正确安装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(Alpine…):
# 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
注意:python3dev不会自动覆盖python3的所有次要版本,如果您正在使用例如python3.8,则可能需要安装python3.8-dev。
这意味着Python.h不在编译器的默认包含路径中。您是在系统范围内安装还是在本地安装?你的操作系统是什么?
您可以使用-I<path>标志来指定编译器应该在其中查找标头的附加目录。您可能需要使用-L<path>进行后续操作,以便gcc可以使用-L<name>找到要链接的库。
你必须做两件事。
安装Python开发包,如果是Debian/Uubuntu/Mint,可以使用以下命令完成:
sudo apt-get install python-dev
第二点是,默认情况下,include文件不在include路径中,Python库默认情况下也不与可执行文件链接。您需要添加这些标志(相应地替换Python的版本):
-I/usr/include/python2.7 -lpython2.7
换句话说,编译命令应该是:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc
确保Python开发文件随操作系统一起提供。
您不应该硬编码库和包含路径。相反,使用pkg配置,它将为您的特定系统输出正确的选项:
$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7
您可以将其添加到gcc行:
gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2)
我设法解决了这个问题,并在一个命令中生成.so文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c
在Ubuntu上,我运行的是Python 3,我必须安装
sudo apt-get install python3-dev
如果要使用未链接到python3的Python版本,请安装相关的python3.x-dev包。例如:
sudo apt-get install python3.5-dev
如果您使用tox在多个版本的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
尝试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#
现在你可以做出专家猜测,从中选择哪一个。
对我来说,把它改成这个效果很好:
#include <python2.7/Python.h>
我找到了文件/usr/include/python2.7/Python.h,因为/usr/include已经在include路径中,所以python2.7/Python.h应该就足够了。
您还可以从命令行添加include路径,而不是-gcc-I/usr/lib/python2.7(谢谢@erm3nda)。
在我的例子中,在Ubuntu中修复它的方法是安装libpython-all-dev(如果您使用Python3,则安装libpypython3-all-dev)包。
当然,python-dev或libpython-all-dev是(apt)安装的第一件事,但如果这对我的情况没有帮助,我建议您通过sudo apt-get-install libffi-dev和sudo pip-install cffi安装外部函数接口包。
这将有助于解决问题,尤其是当您看到错误为/from c/_cffi_backend.c:2:20:致命错误:Python.h:没有这样的文件或目录时。
Cygwin解决方案
您需要安装python2-devel或python3-devel包,具体取决于您使用的Python版本。
您可以使用Cygwin.com上的32位或64位setup.exe(取决于您的安装)快速安装它。
示例(如果需要,请修改setup.exe的文件名和Python的主要版本):
$ setup.exe -q --packages=python3-devel
您还可以查看我的另一个答案,以获得从命令行安装Cygwin软件包的更多选项。
如果您使用带有3.6 python的virtualenv(现在是边缘),请确保安装匹配的python 3.6 dev sudo apt-get install python3.6-dev,否则执行sudo python3 dev将安装python dev 3.3.3-1,这不会解决问题。
这不是同一种情况,但它也适用于我,现在我可以在Python3.5中使用SWIG:
我试图编译:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
使用Python 2.7可以很好地工作,而不是使用我的3.5版本:
exist_wrap.c:147:21:致命错误:Python.h:不存在el archivo o eldirectorio编译已终止。
在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/
当我尝试在CentOS 7上使用Python3.6安装ctds时发生此错误。我完成了这里提到的所有技巧,包括yum安装python34-devel。问题是Python.h在/usr/include/python3.4m中找到,但在/usr/include/python3.6m中找不到。我尝试使用--global选项指向include-dir(pip3.6 install--global option=build_ext--global options=“--include-dirs=/usr/include/pyton3.4m”ctds)。这导致在链接ctd时找不到lpython3.6m。
最后,工作是修复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的include路径中。无论使用哪种版本的python,例如,如果它是3.6,那么它通常应该在/usr/include/python3.6m/python.h中。
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
当您尝试删除python3.5并安装python3.6时,通常会出现这种情况。
因此,当使用python3(即python3-V=>python3.6)安装某些所需的包时,python3.5标头将显示此错误。
通过安装python3.6-dev模块解决。
我在ubuntu中安装coolprop时也遇到了这个错误。
对于ubuntu 16.04和python 3.6
sudo apt-get install python3.6-dev
如果这不起作用,请尝试安装/更新gcc-lib。
sudo apt-get install gcc
有时,即使在安装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
如果操作系统附带的Python未随附,则必须在操作系统上安装Python开发文件。关于这个问题的许多答案显示了在不同系统上实现这一点的多种方式。当您这样做时,问题是告诉编译器它们的位置以及如何针对它们进行编译。Python附带了一个名为Python-config的程序。对于编译,您需要--includes输出,而对于将程序与Python库链接(将Python嵌入到程序中),则需要--ldflags输出。例子:gcc-c mypythonprogram.c$(python3配置--包含)gcc-o程序mypythonprogram.o$(python3配置--ldflags)
python-config程序可以以python版本命名,例如在Debian、Ubuntu上,可以将其命名为python3-config或python3.6-config。
特别是对于Python 3.7和Ubuntu,我需要
sudo apt安装libpython3.7-dev
.我想在某些时候,名字从pythom.n-dev改成了这个。
对于Python 3.6、3.8到3.10(以及计数…),类似地:
sudo apt安装libpython3.6-dev
sudo apt安装libpython3.8-dev
sudo apt安装libpython3.9-dev
sudo apt安装libpython3.10-dev
尝试定位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或python3dev;否则包括编译器的正确标头路径:
g++ -I/usr/include/python3.8 ...
若您使用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})
这里还有另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在AmazonLinuxAMI基础Docker镜像上安装Python 3.6的一些东西。
非码头解决方案:
# 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映像。例如,我试图在python:3.9.4-slim上安装asyncpg~=0.24.0,这产生了与您看到的相同的错误。然而,当我将图像更新为python:3时,它工作得很好。
当您安装了不同的Python版本,并且使用的不是系统的pip时,也会出现此问题。在这种情况下,非系统pip无法找到正确版本的Python头。
我在尝试pip安装与应用程序捆绑的Python包时遇到了这种情况。由于不是系统的python,apt-installpythonXX-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/binder 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>
我遇到的情况是我的Python.h位于/usr/include/python3.8和/usr/include/pyton2.7目录中,只需通过类似的方式给出gcc的路径-I/usr/include/pymon3.8即可python的版本替换为您的版本
我在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-filename。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=)
经确认,如果您正在运行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
如果您已经安装了Python,并且希望链接到特定的Python版本,则可以从Python获取相关的include路径。
>>> 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库头。如果您使用Linux Solus,可以使用以下命令解决此问题:
sudo eopkg install python3-devel