简体   繁体   English

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

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

I try to using PythonInterpreter in Java but I can't import org.python.util.PythonInterpreter package in Intellij.我尝试在 Java 中使用 PythonInterpreter,但我无法在 Intellij 中导入 org.python.util.PythonInterpreter package。 So the code below can't compile:所以下面的代码无法编译:

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");
    }
}

I tried to download jython zip library but Intellij not recognize this library.我尝试下载 jython zip 库,但 Intellij 无法识别该库。 Any suggest for me?对我有什么建议吗?

Make sure that you have installed Jython correctly into the project.确保已将Jython正确安装到项目中。

  • The jar file should be downloaded from the page . jar 文件应从该页面下载。 In your case I recommend to use Jython Standalone option.在您的情况下,我建议使用Jython Standalone选项。

  • Add the jar file as a library according to the official JetBrains guide .根据 JetBrains 官方指南,将 jar 文件添加为库。

If everything is fine with the project configuration but the problem remains you can try to perform the following actions:如果项目配置一切正常但问题仍然存在,您可以尝试执行以下操作:

  • File | Invalidate Caches / Restart...

  • Close the IDE.关闭 IDE。

  • Remove all .idea files from the project.从项目中删除所有.idea文件。

  • Remove/Rename IntelliJ IDEA cache files according to the following guide .根据以下指南删除/重命名 IntelliJ IDEA 缓存文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM