python - 从 Django 导入 .so 文件

标签 python c++ django

我有一个C++代码

#include <Python.h>

static PyObject* py_veripy(PyObject* self, PyObject* args){

    /* BODY */
    return Py_BuildValue("i", 1);

}

// Bind Python function names to our C functions
static PyMethodDef veripy_methods[] = {
    {"veripy", py_veripy, METH_VARARGS},
    {NULL, NULL}
};

// Python calls this to let us initialize our module
extern "C" void initveripy(){
    (void) Py_InitModule("veripy", veripy_methods);
}

我已经编译它并创建了一个 .so 文件。我想在我的一个 Django 应用程序中使用这个库,但我对将这个库保存在我的目录中的什么位置感到困惑。显然,我已经尝试将它与代码保持在一起,但它似乎没有找到它。我找到了 this教程类似于我的案例,但那里的 C++ 代码中没有包含 Python.h。请帮忙。

最佳答案

我认为您应该创建一个 setup.py 文件,以便编译您的库并将其放在某个地方。

例如,这里是我用于个人项目的 setup.py 文件示例:

from setuptools import setup, Extension

setup(
      name="modulename", 
      version="0.1",
      test_suite = "nose.collector",
      author='<author name>',
      setup_requires=['nose>=0.11'],
      description="My super module implementation",
      ext_modules=[Extension("modulename", ["src/mysupermodule.c"])]
)

这将生成一个“build/”文件夹,当然您可以在 the official documentation page 上找到有关设置工具的更多信息.

关于python - 从 Django 导入 .so 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200020/

相关文章:

python - django 错误 - ImportError : No module named apps

django - DRF : Using 'SlugRelatedField' on the 'ImageField'

python - 日期列中的 Pandas 多个日期范围

python - 在 Spark : Executor lost 中获取错误

c++ - 对 CGAL 类型使用基于范围的 for 循环

c++ - C++11编译器的构造函数继承产生错误

c++ - 在 OpenGL 对象包装器中自动绑定(bind)

python - 使用 python flask socketio 向除发件人之外的所有连接的客户端广播

python - 为什么 imaplib 在每封电子邮件后返回一个单独的括号?

python - 如何使用 Django 创建用户到用户的消息系统?