尝试编译 C 扩展模块时缺少 Python.h
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4097339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing Python.h while trying to compile a C extension module
提问by Jonathan
I'm following this tutorialon how to extend Python with C\C++ code.
我正在关注有关如何使用 C\C++ 代码扩展 Python 的教程。
The section named "Building the extension module with GCC for Microsoft Windows" fails for me with the following error:
名为“使用 GCC 为 Microsoft Windows 构建扩展模块”的部分对我来说失败并出现以下错误:
fatal error: Python.h: No such file or directory
The section named "Building the extension module using Microsoft Visual C++" also fails with a similar error:
名为“使用 Microsoft Visual C++ 构建扩展模块”的部分也失败并出现类似错误:
fatal error C1083: Cannot open include file: 'Python.h': No such file or directory
What should I do to solve this?
我该怎么做才能解决这个问题?
采纳答案by stonemetal
- Do you have the python dev files so that you can find Python.h?
- Do you have the location of Python.h specified to your compiler? with gcc this is usually done through a -I path to include.
- 你有 python dev 文件以便你可以找到 Python.h 吗?
- 您是否为编译器指定了 Python.h 的位置?对于 gcc,这通常是通过包含的 -I 路径完成的。
Figuring out which of those is failing will solve your problem.
找出哪些失败将解决您的问题。
from the article you linked:
来自您链接的文章:
gcc -c hellomodule.c -I/PythonXY/include
gcc -c hellomodule.c -I/PythonXY/include
gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll
gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll
They assumed you installed python in the default location c:\pythonXY(Where X is the major version number and Y is the minor version number).(in your case Python26) If you put python somewhere else replace /PythonXY with where ever you installed it.
他们假设您在默认位置 c:\pythonXY 安装了 python(其中 X 是主要版本号,Y 是次要版本号)。(在您的情况下为 Python26)如果您将 python 放在其他地方,请将 /PythonXY 替换为您安装的位置它。
回答by Karim
For Linux, Ubuntu users to resolve the issue of missing Python.h while compiling, simply run the following command in your terminal to install the development package of python:
对于Linux、Ubuntu用户,要解决编译时缺少Python.h的问题,只需在终端中运行以下命令即可安装python的开发包:
In Terminal: sudo apt-get install python-dev
在终端: sudo apt-get install python-dev
Good luck
祝你好运
回答by Qin Hongbo
The Python official documentation has already made it clear. Check it out here
Python官方文档已经说得很清楚了。检查出来这里
The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/, where prefix and exec_prefix are defined by the corresponding parameters to Python's configure script and version is '%d.%d' % sys.version_info[:2]. On Windows, the headers are installed in prefix/include, where prefix is the installation directory specified to the installer.
To include the headers, place both directories (if different) on your compiler's search path for includes. Do not place the parent directories on the search path and then use #include ; this will break on multi-platform builds since the platform independent headers under prefix include the platform specific headers from exec_prefix.
头文件通常与 Python 一起安装。在 Unix 上,它们位于目录 prefix/include/pythonversion/ 和 exec_prefix/include/pythonversion/ 中,其中 prefix 和 exec_prefix 由 Python 配置脚本的相应参数定义,版本为 '%d.%d' % sys。版本信息[:2]。在 Windows 上,头文件安装在 prefix/include 中,其中 prefix 是指定给安装程序的安装目录。
要包含头文件,请将两个目录(如果不同)放在编译器的包含搜索路径上。不要将父目录放在搜索路径上,然后使用 #include ;这将在多平台构建中中断,因为前缀下的平台独立标头包括来自 exec_prefix 的平台特定标头。
And they have provided a convenient way to get the correct cflags that we should pass to compiler. here
他们提供了一种方便的方法来获取我们应该传递给编译器的正确 cflags。这里
So for example, here is what I got after running the command
例如,这是我运行命令后得到的
root@36fd2072c90a:/# /usr/bin/python3-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
Pass those flags to the compiler, and it will work.
将这些标志传递给编译器,它就会工作。

