gpt4 book ai didi

java - 使用java运行python

转载 作者:行者123 更新时间:2023-11-30 02:02:41 25 4
gpt4 key购买 nike

如何解决java中的以下错误?

需求:使用java运行python程序

支持库:jython-standalone-2.7.0.jar

已安装Python:Python 3.6.0

public static void main(String[] args) throws FileNotFoundException, ScriptException, IOException {
System.out.println("Hello world!!");
//option 1
StringWriter writer = new StringWriter(); //ouput will be stored here

ScriptEngineManager manager = new ScriptEngineManager();
ScriptContext context = new SimpleScriptContext();

context.setWriter(writer); //configures output redirection

ScriptEngine engine = manager.getEngineByName("python");
PythonInterpreter interpreter = new PythonInterpreter();

interpreter.exec("import sys\nsys.path.append('C:\\Users\\johns\\AppData\\Local\\Programs\\python\\python36\\Lib\\site-packages')");
engine.eval(new FileReader("C:\\Users\\johns\\Desktop\\python\\pytest.py"), context);
System.out.println("");
System.out.println(writer.toString());
//option 2
// String execCmd = execCmd("python
C:\\Users\\johns\\Desktop\\python\\pytest.py");
// System.out.println("From Cmd Prompt" + execCmd);

}

public static String execCmd(String cmd) throws java.io.IOException {
Process proc = Runtime.getRuntime().exec(cmd);
java.io.InputStream is = proc.getInputStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
String val = "";
if (s.hasNext()) {
val = s.next();
} else {
val = "";
}
return val;
}

错误消息:

Exception in thread "main" javax.script.ScriptException: AttributeError: 'tuple' object has no attribute 'major' in <script> at line number 1
at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:47)
at pythonproj.PythonProj.main(PythonProj.java:47)
Caused by: Traceback (most recent call last):
File "<script>", line 1, in <module>
File "C:\Users\johns\AppData\Local\Programs\python\python36\Lib\site-
packages\pytesseract\__init__.py", line 1, in <module>
from .pytesseract import (
File "C:\Users\johns\AppData\Local\Programs\python\python36\Lib\site-
packages\pytesseract\pytesseract.py", line 10, in <module>
from PIL import Image
File "C:\Users\johns\AppData\Local\Programs\python\python36\Lib\site-
packages\PIL\Image.py", line 31, in <module>
from ._util import py3
File "C:\Users\johns\AppData\Local\Programs\python\python36\Lib\site-
packages\PIL\_util.py", line 3, in <module>
py3 = sys.version_info.major >= 3
AttributeError: 'tuple' object has no attribute 'major'

at org.python.core.Py.AttributeError(Py.java:205)
at org.python.core.PyObject.noAttributeError(PyObject.java:1013)
at org.python.core.PyObject.__getattr__(PyObject.java:1008)

建议使用 jython 运行 python 程序或通过调用命令进程作为上面代码中的选项 2 是更好的方法

最佳答案

您遇到的异常似乎是由 a jython bug 引起的(这显然是通过 jython-2.7.1 修复的)。

关于您的其他问题;如果您只想运行脚本而不以任何方式与解释器交互,并且您知道系统上安装了合适的 python 版本,那么使用进程可能会容易得多,并且可以避免此问题以及潜在的其他问题jython。

关于java - 使用java运行python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52316449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com