最初的报错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.(PythonInterpreter.java:116) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
at org.python.util.PythonInterpreter.(PythonInterpreter.java:94) ~[pipeline-0.0.1-SNAPSHOT.jar!/:na]
at org.python.util.PythonInterpreter.(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等模块。