我正在尝试使用 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 开发的头文件和静态库。使用您的包管理器在系统范围内安装它们。
对于 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
注意:python3-dev 不会自动覆盖 python3 的所有次要版本,如果您使用例如 python 3.8,您可能需要安装 python3.8-dev。
在 Ubuntu 上,我运行的是 Python 3,我必须安装
sudo apt-get install python3-dev
如果要使用未链接到 python3 的 Python 版本,请安装关联的 python3.x-dev 包。例如:
sudo apt-get install python3.5-dev
特别是对于 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
你必须做两件事。
安装 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
在 Fedora 上为 Python 2 运行这个:
sudo dnf install python2-devel
对于 Python 3:
sudo dnf install python3-devel
apt-get install python-dev
。
如果您使用 tox 在多个 Python 版本上运行测试,则可能需要为您正在测试的每个 Python 版本安装 Python 开发库。
sudo apt-get install python2.6-dev
sudo apt-get install python2.7-dev
etc.
确保 Python 开发文件随您的操作系统一起提供。
您不应该对库和包含路径进行硬编码。相反,请使用 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)
Cygwin 的解决方案
您需要安装软件包 python2-devel
或 python3-devel
,具体取决于您使用的 Python 版本。
您可以使用 Cygwin.com 中的 32-bit 或 64-bit setup.exe
(取决于您的安装)快速安装它。
示例(如果需要,修改 setup.exe
的文件名和 Python 的主要版本):
$ setup.exe -q --packages=python3-devel
您还可以查看我的 other answer,了解更多从命令行安装 Cygwin 软件包的选项。
对我来说,将其更改为此有效:
#include <python2.7/Python.h>
我找到了文件 /usr/include/python2.7/Python.h
,并且由于 /usr/include
已经在包含路径中,那么 python2.7/Python.h
应该就足够了。
您也可以从命令行添加包含路径 - gcc -I/usr/lib/python2.7
(感谢@erm3nda)。
gcc -I/usr/lib/python2.7 etc
而不是硬编码包含调用。
在 AWS API (centOS) 中
yum install python27-devel
pip install cryptography
时遇到的问题。
AWS EC2 安装运行 python34:
sudo yum install python34-devel
如果您使用带有 3.6 python 的 virtualenv(现在是边缘),请务必安装匹配的 python 3.6 dev sudo apt-get install python3.6-dev
,否则执行 sudo python3-dev
将安装 python dev 3.3.3-1,这将无法解决问题。
就我而言,在 Ubuntu 中修复它的方法是安装软件包 libpython-all-dev
(或 libpython3-all-dev
,如果您使用 Python 3)。
python-all-dev
在我的情况下,但足够接近。
情况不一样,但它也适用于我,现在我可以将 SWIG 与 Python3.5 一起使用:
我试图编译:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
使用 Python 2.7 可以正常工作,而不是使用我的 3.5 版:
exists_wrap.c:147:21: 致命错误: Python.h: No existe el archivo o el Directorio 编译终止。
在我的 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/
我设法解决了这个问题并在一个命令中生成了 .so 文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c
我在ubuntu中安装coolprop时也遇到了这个错误。
对于带有 python 3.6 的 ubuntu 16.04
sudo apt-get install python3.6-dev
如果这不起作用,请尝试安装/更新 gcc
lib。
sudo apt-get install gcc
python3-dev
很多次并不断收到相同的错误,但不知道 python3.6-dev
!谢谢!!
尝试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#
现在,您可以做出专家猜测,选择哪一个。
对于 CentOS 7:
sudo yum install python36u-devel
我按照此处的说明在多个 VM 上安装 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 一起使用
对于那里的 OpenSuse 同志:
sudo zypper install python3-devel
当您安装了不同的 Python 版本并且您使用的 pip 不是系统的 pip 时,也会出现此问题。在这种情况下,非系统 pip 将找不到正确版本的 Python 标头。
当我尝试为与应用程序捆绑的 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>
如果您在 Amazon Linux 上使用 Python 3.6(基于 RHEL,但此处给出的 RHEL 答案不起作用):
sudo yum install python36-devel
这是另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在适用于 Python 3.6 的 Amazon Linux AMI 基础 Docker 映像上pip install
一些东西。
非码头解决方案:
# 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
码头工人解决方案:
# 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-config --includes) gcc -o program mypythonprogram.o $(python3-config --ldflags)
python-config
程序可以以 Python 版本命名 - 例如在 Debian、Ubuntu 上,这些可以命名为 python3-config
或 python3.6-config
。
当然 python-dev
或 libpython-all-dev
是 (apt
)install
的第一件事,但如果这对我的情况没有帮助,我建议您安装 foreign Function Interface 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
的错误时。
尝试找到您的 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 ...
我在 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=)
#include "Python.h"
。编译器看不到它。您应该手动编辑路径。或者您可以使用我发布的 Makefile。此文件与 #include
的手动编辑相同
当我尝试使用 Python3.6 在 CentOS 7 上安装 ctds 时出现此错误。我做了这里提到的所有技巧,包括 yum install python34-devel
。问题是在 /usr/include/python3.4m but not in /usr/include/python3.6m
中发现了 Python.h
。我尝试使用 --global-option
指向包含目录 (pip3.6 install --global-option=build_ext --global-option="--include-dirs=/usr/include/python3.4m" ctds
)。这导致在链接 ctds 时找不到 lpython3.6m
。
最后起作用的是修复 Python3.6 的开发环境需要使用包含和库进行更正。
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
中。
当您尝试删除 python3.5
并安装 python3.6
时,它经常出现。
所以在使用python3
(其中python3 -V
=> python3.6
)安装一些需要的包时需要python3.5
头会出现这个错误。
通过安装 python3.6-dev
模块解决。
有时即使在安装 python-dev 之后错误仍然存在,如果缺少“gcc”,请检查错误。
首先按照https://stackoverflow.com/a/21530768/8687063中的说明下载,然后安装 gcc
对于 apt(Ubuntu、Debian ...):
sudo apt-get install gcc
对于百胜(CentOS,RHEL ...):
sudo yum install gcc
对于 dnf(Fedora ...):
sudo dnf install gcc
对于 zypper (openSUSE...):
sudo zypper in gcc
对于 apk(高山...):
sudo apk gcc
这意味着 Python.h
不在编译器的默认包含路径中。您是在系统范围内还是在本地安装它?你的操作系统是什么?
您可以使用 -I<path>
标志指定编译器应在其中查找标头的附加目录。您可能必须跟进 -L<path>
,以便 gcc 可以找到您将使用 -l<name>
链接的库。
不定期副业成功案例分享