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

gcc -Wall utilsmodule.c -o Utilc

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

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

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


当前回答

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

其他回答

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

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

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

这里还有另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在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.h位于/usr/include/python3.8和/usr/include/pyton2.7目录中,只需通过类似的方式给出gcc的路径-I/usr/include/pymon3.8即可python的版本替换为您的版本

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

对于ubuntu 16.04和python 3.6

sudo apt-get install python3.6-dev

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

sudo apt-get install gcc