我有一个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/