gpt4 book ai didi

java - 如何在Java项目Intellij中使用PythonInterpreter - Jython

转载 作者:行者123 更新时间:2023-12-01 17:29:08 29 4
gpt4 key购买 nike

我尝试在 Java 中使用 PythonInterpreter,但无法在 Intellij 中导入 org.python.util.PythonInterpreter 包。所以下面的代码无法编译:

import org.python.util.PythonInterpreter; 
import org.python.core.*;

public class SimpleEmbedded {
public static void main(String[] args) throws PyException {
PythonInterpreter interp = new PythonInterpreter();

System.out.println("Hello, brave new world");
interp.exec("import sys");
interp.exec("print sys");

interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");

System.out.println("x: "+x);
System.out.println("Goodbye, cruel world");
}
}

我尝试下载 jython zip 库,但 Intellij 无法识别该库。有什么建议给我吗?

最佳答案

确保您已将 Jython 正确安装到项目中。

  • jar 文件应从 page 下载。对于您的情况,我建议使用 Jython Standalone 选项。

  • 根据JetBrains官方guide将jar文件添加为库.

如果项目配置一切正常,但问题仍然存在,您可以尝试执行以下操作:

  • 文件 |使缓存无效/重新启动...

  • 关闭 IDE。

  • 从项目中删除所有 .idea 文件。

  • 根据以下内容删除/重命名 IntelliJ IDEA 缓存文件 guide .

  • 从现有源导入项目。 (文件 | 新建 | 现有来源的项目...)

关于java - 如何在Java项目Intellij中使用PythonInterpreter - Jython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61157199/

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