【问题标题】:Can I call Python library using JNI?我可以使用 JNI 调用 Python 库吗?
【发布时间】:2014-02-23 12:49:08
【问题描述】:

我知道有jython解决方案,但是jython不能加载wtxl和rdxl;它只能加载有限的 pip 库。所以我想写JNI来做。

主要程序如下: Java JNI C/C++ Python 接口 本地 Python 环境 我的 html2excel python 库

问题是导入html2excel库失败。我这样使用 C/C++ 代码:

int to_excel(const std::string & htmlfile)
{
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('/home/allen/python')");
    PyObject *html2excelModule = PyImport_ImportModule("html2excel");
    if (!html2excelModule )
    {
        std::cerr << "Error: open python html2excel failed" << std::endl;
        Py_Finalize();
        return -1;
    }
    ...
}

上面的程序告诉我这个错误

ImportError: 没有名为 html2excel 的模块

html2excel.py 位于 /home/allen/python。从shell运行就可以了。

$python
>>>import sys
>>>sys.path.append('/home/allen/python')
>>>import html2excel

为什么我的 JNI 库无法导入现有的 html2excel 模块?提前谢谢!

【问题讨论】:

  • 你应该检查之前调用的函数的返回码
  • 前面的函数调用成功,都返回0。

标签: java python c++ java-native-interface


【解决方案1】:

错误信息显示 rlm_python:EXCEPT:: /usr/lib/python2.7/lib-dynload/_io.so: undefined symbol: _Py_ZeroStruct

libpython2.7.so 应在 JNI 实现中使用 dlopen 手动加载。

详细解决办法https://github.com/FreeRADIUS/freeradius-server/pull/415给出。

【讨论】:

  • 您应该通过添加链接中的相关信息来改进您的答案。
猜你喜欢
  • 1970-01-01
  • 2012-11-16
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多