我试图使用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没有问题。我设法在我的机器上找到了该文件。
当前回答
这里还有另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在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时,它工作得很好。
其他回答
当我尝试在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中。
若您使用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})
看起来您没有正确安装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库头。如果您使用Linux Solus,可以使用以下命令解决此问题:
sudo eopkg 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#
现在你可以做出专家猜测,从中选择哪一个。