如何在Mac OSX上将python.h添加到我的Python虚拟环境中?

9
我正在为Python应用程序编写C扩展,并需要测试特定于Python的C代码。为此,我需要将Python.h导入到我的C文件中,但是我一直无法做到这一点。大多数教程建议类似于sudo apt-get python-dev,但是我的系统没有apt-get,即使有,我认为在我的虚拟环境中拥有dev文件也更好。你有什么办法将Python.h导入到我的虚拟环境中吗?
3个回答

10

请将环境变量C_INCLUDE_PATH设置为包含您的Python分发目录的路径。

示例: 1. 获取您的Python分发目录的包含目录

    find /usr/local/Cellar/ -name Python.h

这将返回类似于以下内容:
    /usr/local/Cellar//python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/Python.h

现在设置您的C_INCLUDE_PATH变量:
    export C_INCLUDE_PATH="/usr/local/Cellar//python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/"

我需要在 macOS Ventura 13.4.1 上使用 find /opt/homebrew/Cellar -name Python.h 命令。我没有 /usr/local/Cellar/ 目录。 - Stéphan van Biljon

3
假设您使用的是MacOSX操作系统,且已通过homebrew安装了Python。在终端中,您可以通过以下命令找到Python.h文件:
``` $ find /usr/local/Cellar -name Python.h ```
find /usr/local/Cellar/ -name Python.h

返回在我的系统上:

/usr/local/Cellar//python/2.7.6/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h

3
最初的回答
在终端中,
$python-config --includes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接