我试图使用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没有问题。我设法在我的机器上找到了该文件。
当前回答
当您尝试删除python3.5并安装python3.6时,通常会出现这种情况。
因此,当使用python3(即python3-V=>python3.6)安装某些所需的包时,python3.5标头将显示此错误。
通过安装python3.6-dev模块解决。
其他回答
如果您已经安装了Python,并且希望链接到特定的Python版本,则可以从Python获取相关的include路径。
>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/include/python3.10' # Example output
您可以使用shell脚本手动指定特定python版本的包含路径/构建:
#!/bin/sh
python_include=$(python3.10 -c "import sysconfig; print(sysconfig.get_path('include'))")
gcc -Wall utilsmodule.c -o Utilc -I"$python_include"
确保Python开发文件随操作系统一起提供。
您不应该硬编码库和包含路径。相反,使用pkg配置,它将为您的特定系统输出正确的选项:
$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7
您可以将其添加到gcc行:
gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2)
特别是对于Python 3.7和Ubuntu,我需要
sudo apt安装libpython3.7-dev
.我想在某些时候,名字从pythom.n-dev改成了这个。
对于Python 3.6、3.8到3.10(以及计数…),类似地:
sudo apt安装libpython3.6-dev
sudo apt安装libpython3.8-dev
sudo apt安装libpython3.9-dev
sudo apt安装libpython3.10-dev
你必须做两件事。
安装Python开发包,如果是Debian/Uubuntu/Mint,可以使用以下命令完成:
sudo apt-get install python-dev
第二点是,默认情况下,include文件不在include路径中,Python库默认情况下也不与可执行文件链接。您需要添加这些标志(相应地替换Python的版本):
-I/usr/include/python2.7 -lpython2.7
换句话说,编译命令应该是:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc
如果您使用带有3.6 python的virtualenv(现在是边缘),请确保安装匹配的python 3.6 dev sudo apt-get install python3.6-dev,否则执行sudo python3 dev将安装python dev 3.3.3-1,这不会解决问题。