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

           目前的項目用到了插件開發,需要在程序裏使用插件,實現功能的定製化。目前使用了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等模塊。




   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章