这个错误信息表明在尝试打开文件 ./python_plugin.so
时,系统找不到该文件。这种情况通常发生在以下几种场景中:
.so
文件是 Linux 系统中的共享库文件,类似于 Windows 中的 DLL 文件。它们包含可由多个程序共享的代码和数据。python_plugin.so
文件。chmod
命令修改权限:chmod
命令修改权限:假设你有一个简单的 Python 插件,你可以这样加载和使用它:
python_plugin.c
#include <Python.h>
static PyObject* hello_world(PyObject* self, PyObject* args) {
printf("Hello, World!\n");
Py_RETURN_NONE;
}
static PyMethodDef PluginMethods[] = {
{"hello_world", hello_world, METH_VARARGS, "Print 'Hello, World!'"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef pluginmodule = {
PyModuleDef_HEAD_INIT,
"python_plugin", // Module name
NULL, // Module documentation
-1, // Size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
PluginMethods
};
PyMODINIT_FUNC PyInit_python_plugin(void) {
return PyModule_Create(&pluginmodule);
}
编译共享库
gcc -shared -o python_plugin.so -fPIC python_plugin.c $(python3-config --includes)
Python 代码
import ctypes
# Load the shared library
plugin = ctypes.CDLL('./python_plugin.so')
# Call the function
plugin.hello_world()
通过以上步骤,你应该能够解决 open("./python_plugin.so"): no such file or directory
错误。
没有搜到相关的文章