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

gcc -Wall utilsmodule.c -o Utilc

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

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

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


当前回答

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

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

其他回答

我在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 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中。

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

这里还有另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在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未随附,则必须在操作系统上安装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。