我试图使用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。
其他回答
在我的例子中,在Ubuntu中修复它的方法是安装libpython-all-dev(如果您使用Python3,则安装libpypython3-all-dev)包。
在Ubuntu上,我运行的是Python 3,我必须安装
sudo apt-get install python3-dev
如果要使用未链接到python3的Python版本,请安装相关的python3.x-dev包。例如:
sudo apt-get install python3.5-dev
问题可能是您没有安装适当的Python库头。如果您使用Linux Solus,可以使用以下命令解决此问题:
sudo eopkg install python3-devel
我在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=)
对我来说,把它改成这个效果很好:
#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)。