Java执行Python代码报错console: Failed to install java.nio.charset.UnsupportedCharsetException: cp0

来源:互联网 发布:linux下一行 编辑:程序博客网 时间:2024/06/06 06:52

代码:

PythonInterpreter interpreter = new PythonInterpreter();  interpreter.execfile("D:\\softwares\\pycharm\\untitled2\\test.py");

报错信息:

console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.

Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['D:\\.m2\\repository\\org\\python\\jython\\2.7.0\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: D:\.m2\repository\org\python\jython\2.7.0
    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


在代码中添加:

Properties props = new Properties();        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]);                PythonInterpreter interpreter = new PythonInterpreter();          interpreter.execfile("D:\\softwares\\pycharm\\untitled2\\test.py");

这样就不会报错了。

0 0
原创粉丝点击