【问题标题】:How to use PythonInterpreter in Java project Intellij - Jython如何在 Java 项目 Intellij 中使用 PythonInterpreter - Jython
【发布时间】:2020-04-11 12:59:39
【问题描述】:

我尝试在 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 无法识别该库。对我有什么建议吗?

【问题讨论】:

    标签: java intellij-idea jython


    【解决方案1】:

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

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

    • 按照JetBrains官方guide将jar文件添加为库。

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

    • File | Invalidate Caches / Restart...

    • 关闭 IDE。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多