java可以调用python_从Java调用Python模块

袁建木
2023-12-01

I have a Python interface of a graph library written in C - igraph (the name of library). My need is to invoke the python modules pertaining to this graph library from Java code. It goes like this, the core of library is in c. This core has been imported into Python and interfaces to the functions embedded in core are available in Python. My project's rest of the code is in Java and hence I would like to call the graph functions by Java as well. Jython - which lets you invoke python modules with in Java was an option.I went on trying Jython to discover that it will not work in my case as the core code is in C and Jython wont support anything that is imported as a c dll in python code.I also thought of opting for the approach of calling graph routines directly in c. That is without passing through Python code. I am assuming there must be something which lets you call c code from Java, how ever I am not good in C hence I did not go for it. My last resort seems to execute Python interpreter from command line using Java. But that is a dirty and shameless. Also to deal with the results produced by Python code I will have to write the results in a file and read it back in java. Again dirty way. Is there something that any one can suggest me? Thanks to every one giving time.

感谢Igal回答。我看了一眼。乍一看,它似乎只是简单地调用python脚本。Jep jep = new Jep(false, SCRIPT_PATH, cl);

jep.set("query", query);

jep.runScript(SCRIPT_PATH + file);

jep.close();

它与我们从命令行通过Java代码调用python解释器时的操作非常相似。Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec("python test.py");

关注的是如何使用Python脚本生成的结果。天真的方法是将它们写入文件并用Java读回。我正在寻找一种更聪明的方法。无论如何,谢谢你的建议。

 类似资料: