我正在为 python 应用程序编写 C 扩展,并且需要测试特定于 python 的 C 代码。为此,我需要将 Python.h 导入到我的 C 文件中,但对于我来说,我一直无法做到这一点。大多数教程都提出了类似 sudo apt-get python-dev 的建议,但我的系统没有 apt-get,即使有,我认为最好将 dev 文件放在我的 virtualenv 中。
知道如何让 Python.h 进入我的 virtualenv 吗?
我正在为 python 应用程序编写 C 扩展,并且需要测试特定于 python 的 C 代码。为此,我需要将 Python.h 导入到我的 C 文件中,但对于我来说,我一直无法做到这一点。大多数教程都提出了类似 sudo apt-get python-dev 的建议,但我的系统没有 apt-get,即使有,我认为最好将 dev 文件放在我的 virtualenv 中。
知道如何让 Python.h 进入我的 virtualenv 吗?
设置环境变量 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/"
假设您使用的是通过 homebrew 安装的 MacOSX 和 Python,
在 中terminal
,您可以找到 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
在终端,
$python-config --includes