iro*_*ein 6 c python python-c-api python-2.7 python-extensions
我正在为 python 编写 C 扩展。我暂时只是在尝试,我已经编写了一个 hello world 扩展,如下所示:
#include <Python2.7/Python.h>
static PyObject* helloworld(PyObject* self)
{
return Py_BuildValue("s", "Hello, Python extensions!!");
}
static char helloworld_docs[] = "helloworld( ): Any message you want to put here!!\n";
static PyMethodDef helloworld_funcs[] = {
{"helloworld", (PyCFunction)helloworld, METH_NOARGS, helloworld_docs},
{NULL,NULL,0,NULL}
};
void inithelloworld(void)
{
Py_InitModule3("helloworld", helloworld_funcs,"Extension module example!");
}
Run Code Online (Sandbox Code Playgroud)
从我编写的 setup.py 文件安装它并从命令行安装之后,代码工作得很好
python setup.py install
Run Code Online (Sandbox Code Playgroud)
我想要的是以下内容:
我想使用 C 文件作为 python 扩展模块,而不安装它,也就是说,我想将它用作项目中的另一个 python 文件,而不是在我的 python 模块使用其之前需要安装的文件功能。有什么方法可以做到这一点吗?
您可以简单地编译扩展而无需安装(通常类似于python setup.py build)。然后,您必须确保解释器可以找到已编译的模块(例如,通过将其复制到导入它的脚本旁边,或设置PYTHONPATH)。