您的位置:首页 > 编程语言 > Python开发

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

2016-12-27 19:30 726 查看
代码:

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");


这样就不会报错了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐