簡體   English   中英

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

[英]How to use PythonInterpreter in Java project Intellij - Jython

我嘗試在 Java 中使用 PythonInterpreter,但我無法在 Intellij 中導入 org.python.util.PythonInterpreter package。 所以下面的代碼無法編譯:

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 文件應從該頁面下載。 在您的情況下,我建議使用Jython Standalone選項。

  • 根據 JetBrains 官方指南,將 jar 文件添加為庫。

如果項目配置一切正常但問題仍然存在,您可以嘗試執行以下操作:

  • File | Invalidate Caches / Restart...

  • 關閉 IDE。

  • 從項目中刪除所有.idea文件。

  • 根據以下指南刪除/重命名 IntelliJ IDEA 緩存文件。

  • 從現有來源導入項目。 (File | New | Project from Existing Sources...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM