如何在不使用 ctype Linux 的情况下将 so 文件作为模块导入?

发布于 2025-01-17 08:21:54 字数 1502 浏览 4 评论 0原文

文件结构文件夹/home/cyan/TEMP

test.py
  
lib
 |--libc_test_module.so
 
c_test_module.cc
 
CMakeLists.txt

test.py

import sys
sys.path.append("/home/cyan/TEMP/lib") 
import c_test_module

c_test_module.cc

#include <Python.h>

int c_test_function(int a) {
    return a + 1;
}

static PyObject * _c_test_function(PyObject *self, PyObject *args)
{
    int _a;
    int res;

    if (!PyArg_ParseTuple(args, "i", &_a))
        return NULL;
    res = c_test_function(_a);
    return PyLong_FromLong(res);
}


/*  define functions in module */
static PyMethodDef TestMethods[] =
{
     {"c_test_function", _c_test_function, METH_VARARGS,
         "this is a c_test_function"},
     {NULL, NULL, 0, NULL}
};


static struct PyModuleDef c_test_module = {
    PyModuleDef_HEAD_INIT,
    "c_test_module", "Some documentation",
    -1,
    TestMethods
};
PyMODINIT_FUNC PyInit_c_test_module(void) {
    PyObject *module;
    module = PyModule_Create(&c_test_module);
    if(module==NULL) return NULL;
    /* IMPORTANT: this must be called */
    import_array();
    if (PyErr_Occurred()) return NULL;
    return module;
}

错误

ModuleNotFoundError: No module named 'c_test_module'

问题

我不想使用ctype模块导入.so文件。相反,我想知道是否有办法直接将此文件作为模块导入。

File structure under folder /home/cyan/TEMP

test.py
  
lib
 |--libc_test_module.so
 
c_test_module.cc
 
CMakeLists.txt

test.py

import sys
sys.path.append("/home/cyan/TEMP/lib") 
import c_test_module

c_test_module.cc

#include <Python.h>

int c_test_function(int a) {
    return a + 1;
}

static PyObject * _c_test_function(PyObject *self, PyObject *args)
{
    int _a;
    int res;

    if (!PyArg_ParseTuple(args, "i", &_a))
        return NULL;
    res = c_test_function(_a);
    return PyLong_FromLong(res);
}


/*  define functions in module */
static PyMethodDef TestMethods[] =
{
     {"c_test_function", _c_test_function, METH_VARARGS,
         "this is a c_test_function"},
     {NULL, NULL, 0, NULL}
};


static struct PyModuleDef c_test_module = {
    PyModuleDef_HEAD_INIT,
    "c_test_module", "Some documentation",
    -1,
    TestMethods
};
PyMODINIT_FUNC PyInit_c_test_module(void) {
    PyObject *module;
    module = PyModule_Create(&c_test_module);
    if(module==NULL) return NULL;
    /* IMPORTANT: this must be called */
    import_array();
    if (PyErr_Occurred()) return NULL;
    return module;
}

Error

ModuleNotFoundError: No module named 'c_test_module'

Question

I don't want to use ctype module to import .so file. Instead, I wonder if there is a way to import this file as a module directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2025-01-24 08:21:54

我创建了一个 setup.py 文件:

from distutils.core import setup, Extension
import numpy

def main():
    setup(name="c_test_module",
          version="1.0.0",
          description="Python interface for the c_test_module C library function",
          author="cyan",
          author_email="[email protected]",
          ext_modules=[Extension("c_test_module", ["c_test_module.cc"],
                                include_dirs=[numpy.get_include()])],
    )

if __name__ == "__main__":
    main()

现在可以导入 c_test_module 并调用函数。

I created a setup.py file:

from distutils.core import setup, Extension
import numpy

def main():
    setup(name="c_test_module",
          version="1.0.0",
          description="Python interface for the c_test_module C library function",
          author="cyan",
          author_email="[email protected]",
          ext_modules=[Extension("c_test_module", ["c_test_module.cc"],
                                include_dirs=[numpy.get_include()])],
    )

if __name__ == "__main__":
    main()

Now it works to import c_test_module and call the functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文