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



我尝试在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");
}
}

我试图下载jythonzip库,但Intellij无法识别此库。有什么建议吗?

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

  • 应该从页面下载jar文件。在您的情况下,我建议使用Jython Standalone选项。

  • 根据JetBrains官方指南,将jar文件添加为库。

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

  • File | Invalidate Caches / Restart...

  • 关闭IDE。

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

  • 根据以下指南删除/重命名IntelliJ IDEA缓存文件。

  • 从现有源导入项目。(File | New | Project from Existing Sources...)

相关内容

  • 没有找到相关文章

最新更新