java解析python插件,jython打包部署报错org.python.core.PyException: null

69 篇文章 0 订阅
2 篇文章 0 订阅
           目前的项目用到了插件开发,需要在程序里使用插件,实现功能的定制化。目前使用了python插件,但在java使用python插件解析python文件时,发生了问题。该问题在idea编辑器里没有出现。但把项目打成jar包时,无论是部署在windows机器上,还是部署在linux机器上,都会报错。下面讲一下处理方法。

          最初的报错1:

nested exception is ImportError: Cannot import site module and its dependencies: No module named site
* sys.path: ['/root/ocp/0.5.0/adapter-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: /root/ocp/0.5.0/adapter-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module
] with root cause

org.python.core.PyException: null
	at org.python.core.Py.ImportError(Py.java:328) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at org.python.core.Py.importSiteIfSelected(Py.java:1563) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:116) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:94) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:71) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at com.iflytek.ocp.pipes.module.ScriptModule.getPythonInterpreter(ScriptModule.java:129) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at com.iflytek.ocp.pipes.module.ScriptModule.processPython(ScriptModule.java:136) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at com.iflytek.ocp.pipes.module.ScriptModule.parse(ScriptModule.java:171) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at com.iflytek.ocp.pipes.parser.PipeExecuter.runPipe(PipeExecuter.java:191) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
	at com.iflytek.ocp.adapter.AdapterApplication.onLineSearch(AdapterApplication.java:111) ~[classes!/:0.0.1-SNAPSHOT]
	at com.iflytek.ocp.adapter.AdapterApplication.getConetent(AdapterApplication.java:75) ~[classes!/:0.0.1-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_55]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_55]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_55]
            一开始,以为是代码问题,因为报错位置是ScriptModule.java的129行报错,但是本地运行没问题,排除。

           然后怀疑打包方式有问题,换了个方式,还是不行。由于这方面找不到问题,暂时放弃在这里寻找解决方法。此后只好去百度了,搜索“java整合jython报错”、“spring boot 整合jython报错”等,但是搜到的内容很少,按照网上的方法,解决不了。

           最后,在同事提醒下,去搜索具体的报错信息,就根据“No mudule named site”搜索,果然搜到了结果,中英文结果都有,核心解决方法还是来自英文的解决方案。作为一名英语水平一般的开发人员,确实不能逃避去看英语的网站,中文搜索不到的问题,一般英文是有可能搜索到的,毕竟国外这么多的程序员。

          具体地址:1、http://www.cnblogs.com/kongkaikai/p/5227968.html   2、http://blog.csdn.net/xfei365/article/details/50996727   3、http://bugs.jython.org/issue2355

         核心代码:

Properties props = new Properties();
props.put("python.home","path to the Lib folder");
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false");

Properties preprops = System.getProperties();
		
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interp = new PythonInterpreter();

          但是我在使用过程中又遇到的问题,No mudule named site 问题没有了,但是报No mudule named json。这个问题就比较蛋疼了,网上的解决方式五花八门,但是不适用我的出错。下面贴下我的出错与解决。

         出错2:No mudule named json,与一开始的类似

        处理方式:在代码里import json

        出错3:继续报错,与一开始的类似,报错位置就是添加 import json的位置。

        解决方式,修改home的路径。

        最终代码:

   

 private static PythonInterpreter pyInterpreter = null;

    private static PythonInterpreter getPythonInterpreter() {
        if (pyInterpreter == null) {
            Properties props = new Properties();
            props.put("python.home", "../jython-2.7.0");
            props.put("python.console.encoding", "UTF-8");
            props.put("python.security.respectJavaAccessibility", "false");
            props.put("python.import.site", "false");
            Properties preprops = System.getProperties();
            PythonInterpreter.initialize(preprops, props, new String[0]);
            pyInterpreter = new PythonInterpreter();
            pyInterpreter.exec("import sys");
            pyInterpreter.exec("print 'prefix', sys.prefix");
            pyInterpreter.exec("print sys.path");
            System.out.println("python的jar包引用正确");
            pyInterpreter = new PythonInterpreter();
        }
        return pyInterpreter;
    }
       注意要点:1、使用的jar解压,需要是jython-standalone,否则没有Lib目录;

                           2、python.home不是到达Lib目录,而是上一级的目录,否则找不到json等模块。




   

要在Java中调用Python代码,可以使用Jython库。Jython是一个用于在Java虚拟机上运行Python代码的库。它允许您在Java中导入和调用Python模块。 下面是一个使用Jython库在Java中调用Python代码的例子: 1. 首先,您需要将Jython库添加到Java项目中。您可以从Jython官方网站(https://www.jython.org/download)下载Jython安装包并解压缩它。 2. 然后,您需要在Java代码中导入Jython库: ```java import org.python.core.PyObject; import org.python.util.PythonInterpreter; ``` 3. 接下来,您可以使用以下代码在Java中调用Python代码: ```java // 创建Python解释器 PythonInterpreter interpreter = new PythonInterpreter(); // 执行Python代码 interpreter.exec("print('Hello, World!')"); // 调用Python函数 PyObject someFunction = interpreter.get("some_function"); PyObject result = someFunction.__call__(); // 将Python对象转换为Java对象 String strResult = (String) result.__tojava__(String.class); ``` 4. 如果您想在Java中使用Runtime.getRuntime()来运行Python代码,可以使用以下代码: ```java // 创建Runtime对象 Runtime rt = Runtime.getRuntime(); // 创建Process对象来运行Python脚本 Process pr = rt.exec("python script.py"); // 获取脚本输出 BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line = null; while ((line = input.readLine()) != null) { System.out.println(line); } ``` 在此示例中,您需要将“script.py”替换为您要运行的Python脚本的名称或路径。请注意,如果Python脚本需要输入,您还需要使用Process对象的输出流将输入发送到脚本中。 希望这可以帮助您在Java中调用Python代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坚持是一种态度

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值