从Java调用python函数的不同/更好的方法 [英] Different / better approaches for calling python function from Java

查看:352
本文介绍了从Java调用python函数的不同/更好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我试图从java调用python的函数。

I am quite new to python and am trying to call python's function from java.

我的主要要求是:


  • 调用应该是透明的,因为它不应该只需要修改 .py 文件来启用它java的。我可能会给它任何包含一些函数的python文件。我应该可以调用任何这些函数而无需修改 .py 文件。

  • 我希望能够发送参数两种原始类型( int String 浮动等。)或非原始类型( HashMap ArrayList )从java到python函数并接收返回的对象(其中)可以是原始类型或非原始类型)从python到java。我也在使用pandas DataFrame和numpy ndarray,因此也希望能够在java中发送和接收相应的对象。

  • 最好想要坚持到CPython而不是Jython,因为我可能需要使用Jython中可能不可用的新库。

  • call should be transparent, in the sense that it should not require modifying .py file simply to enable it to be called from java. I might be given any python file with some functions inside it. I should be able to call any of these functions without requiring to modify .py file.
  • I want to be able to send arguments of both primitive types (int, String, floats etc.) or non primitive types (HashMap,ArrayList) from java to python function and receive back the returned object (which may of primitive types or non-primitive types) from python to java. I am also using pandas DataFrame and numpy ndarray and hence also want to be able to send and receive corresponding objects to and from java.
  • I preferably want to stick to CPython instead of Jython because I might need to use newer libraries that might not be available in Jython.

我有几个选项在网上找到。很少是:

There are several options that I found online. Few are:


  • 使用Jython的 PythonInterpreter ,我可以使用它来调用python函数而无需对 .py进行任何更改脚本文件:

py1.py

 def square2(list):
     squares = []
     for i in list:
         squares.append(i*i)
     return squares

JythonTest.groovy

 import org.python.util.PythonInterpreter
 import org.python.core.*;

 class JythonTest
 {
      static main(def args)
      {
          PythonInterpreter pi = new PythonInterpreter()
          pi.exec("from py1 import square2")
          PyFunction pf = (PyFunction)pi.get("square2")
          println pf.__call__(new PyList([1,2,3,4]))[2]   //9
      }
 }

我很满意我的需要。但它不是CPython。

I am very well able to satisfy my needs. But its not CPython.

使用 ScriptEngine :这与PythonInterpreter非常相似。但它的Jython再次出现。此外,与PythonInterpreter不同,我们无法使用Jython 2.5+并且无法直接访问PyObjects。所以这个选项可以很好地关闭。

Use ScriptEngine: This is very similar to PythonInterpreter. But agains its Jython. Also, unlike PythonInterpreter, we cannot work with Jython 2.5+ and cannot directly access PyObjects. So this option can be very well closed.

如果我正确理解了上述所有方法,似乎Jython PythonInterpreter是最佳选择。抓住他们的时候我犯了错误吗?还有其他更好的选择吗?

It seems that Jython PythonInterpreter is the best choice if I have understood all above approaches correctly. Have I made mistakes while grasping them? Also is there any other better option?

推荐答案

目前没有这个问题的答案。使用CPython依赖于Python字节码的执行,而Python字节码又需要将Python解释器嵌入到执行环境中。由于没有Java运行时附带嵌入式Python解释器,所以看起来好像Jython是最好的答案。

There is no current answer to this problem. Using CPython relies on the execution of Python bytecodes, which in turn requires that the Python interpreter be embedded in the execution environment. Since no Java runtime comes with an embedded Python interpreter, it really does look as though Jython is the best answer.

有时你想要的答案是不可用的!

Sometimes the answer you want just isn't available!

这篇关于从Java调用python函数的不同/更好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆