本文翻译自:fatal error: Python.h: No such file or directory
I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below: 我正在尝试使用C扩展文件构建共享库,但首先我必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc
After executing the command, I get this error message: 执行命令后,我得到以下错误消息:
utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated. utilsmodule.c:1:20:致命错误:Python.h:没有此类文件或目录编译终止。
in fact I have tried all the suggested solutions over the internet but the problem still exists ... also I have no problem with Python.h
. 实际上,我已经尝试了所有建议的解决方案,但是仍然存在该问题……对于Python.h
我也没有问题。 I managed to locate the file on my machine ... anybody has faced the same problem before?? 我设法在我的机器上找到该文件……以前有人遇到过同样的问题吗??
#1楼
参考:https://stackoom.com/question/1SL5N/致命错误-Python-h-没有这样的文件或目录
#2楼
Looks like you haven't properly installed the header files and static libraries for python dev. 看来您尚未正确安装python dev的标头文件和静态库。 Use your package manager to install them system-wide. 使用软件包管理器在系统范围内安装它们。
For apt
( Ubuntu, Debian... ): 对于apt
( Ubuntu,Debian ... ):
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
For yum
( CentOS, RHEL... ): 对于yum
( CentOS,RHEL ... ):
sudo yum install python-devel # for python2.x installs
sudo yum install python3-devel # for python3.x installs
For dnf
( Fedora... ): 对于dnf
( Fedora ... ):
sudo dnf install python2-devel # for python2.x installs
sudo dnf install python3-devel # for python3.x installs
For zypper
( openSUSE... ): 对于zypper
( openSUSE ... ):
sudo zypper in python-devel # for python2.x installs
sudo zypper in python3-devel # for python3.x installs
For apk
( Alpine... ): 对于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
For apt-cyg
( Cygwin... ): 对于apt-cyg
( Cygwin ... ):
apt-cyg install python-devel # for python2.x installs
apt-cyg install python3-devel # for python3.x installs
#3楼
This means that Python.h
isn't in your compiler's default include paths. 这意味着Python.h
不在编译器的默认包含路径中。 Have you installed it system-wide or locally? 您在系统范围内还是在本地安装了它? What's your OS? 您的操作系统是什么?
You could use the -I<path>
flag to specify an additional directory where your compiler should look for headers. 您可以使用-I<path>
标志指定编译器应在其中查找标头的其他目录。 You will probably have to follow up with -L<path>
so that gcc can find the library you'll be linking with using -l<name>
. 您可能必须跟上-L<path>
以便gcc可以使用-l<name>
找到要链接的库。
#4楼
Two things you have to do. 您必须做两件事。
Install development package for Python, in case of Debian/Ubuntu/Mint it's done with command: 为Debian / Ubuntu / Mint安装适用于Python的开发包,可通过以下命令完成:
sudo apt-get install python-dev
Second thing is that include files are not by default in the include path, nor is Python library linked with executable by default. 第二件事是,默认情况下,包含文件不在包含路径中,Python库也不与可执行文件链接。 You need to add these flags (replace Python's version accordingly): 您需要添加这些标志(相应地替换Python的版本):
-I/usr/include/python2.7 -lpython2.7
In other words your compile command ought to be: 换句话说,您的编译命令应为:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc
#5楼
Make sure that the Python dev files come with your OS. 确保操作系统随附Python开发文件。
You should not hard code the library and include paths. 您不应该对库进行硬编码并包含路径。 Instead, use pkg-config, which will output the correct options for your specific system: 而是使用pkg-config,它将为您的特定系统输出正确的选项:
$ pkg-config --cflags --libs python2 -I/usr/include/python2.7 -lpython2.7
You may add it to your gcc line: 您可以将其添加到您的gcc行:
gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2)
#6楼
I managed to solve this issue and generate the .so file in one command 我设法解决了这个问题,并在一个命令中生成了.so文件
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c