【问题标题】:Calling python module from Java从 Java 调用 python 模块
【发布时间】:2014-03-27 19:27:22
【问题描述】:

我想使用“PythonInterpreter”从 Java 中的 python 模块调用一个函数,这是我的 Java 代码

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.exec("import sys\nsys.path.append('C:\\Python27\\Lib\\site-packages')\nimport helloworld");

PyObject someFunc = interpreter.get("helloworld.getName");

PyObject result = someFunc.__call__();

String realResult = (String) result.__tojava__(String.class);

System.out.println(realResult);

Python 代码 (helloworld.py) 如下:

    from faker import Factory
    fake = Factory.create()

    def getName():
       name = fake.name()
       return name  

我面临的问题是当我调用interpreter.get 时它返回一个空的PyObject。

知道出了什么问题吗? Python 代码在 IDLE 中运行良好

编辑

我只是对下面的代码做了一些改动

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.exec("import sys\nsys.path.append('C:\\Python27\\Lib\\site-packages')\nimport helloworld");


PyInstance wrapper = (PyInstance)interpreter.eval("helloworld" + "(" + ")");  

PyObject result = wrapper.invoke("getName()");

String realResult = (String) result.__tojava__(String.class);

System.out.println(realResult);

我在我的 python 模块中引入了一个类

from faker import Factory

class helloworld:

def init(self):
    fake = Factory.create()

def getName():
    name = fake.name()
    return name 

现在出现以下错误

Exception in thread "main" Traceback (innermost last):
  File "<string>", line 1, in ?
TypeError: call of non-function (java package 'helloworld')

【问题讨论】:

  • 我对 jython 不是很熟悉,但是如果您尝试改用 .get("helloworld") 会发生什么?或者如果你 .getLocals() 怎么办?
  • 不...不走运,抛出 TypeError: call of non-function (instance of 'org.python.core.PyStringMap') with getLocals
  • 如果你在做import helloworld而不是from helloworld import helloworld,类构造函数会不会是helloworld.helloworld()
  • 另外,def init(self): fake = ... 应该是 def __init__(self): self.fake = ...getName 应该采用 self 如果你将假存储在实例变量中。

标签: java python


【解决方案1】:
  1. 您不能在PythonInterpreter.get 中使用python 属性访问。只需使用属性名称获取模块,并从中检索属性。
  2. (编辑部分)Python 代码完全损坏。请参阅下面的正确示例。当然,www.python.org
  3. (EDIT PART)PyInstance.invoke 的第一个参数应该是方法名。
  4. 您可以在下面找到这两种情况的工作代码示例

Main.java

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

public class Main {

    public static void main(String[] args) {
        test1();
        test2();
    }

    private static void test1() {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("import hello_world1");
        PyObject func = interpreter.get("hello_world1").__getattr__("get_name");
        System.out.println(func.__call__().__tojava__(String.class));
    }

    private static void test2() {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from hello_world2 import HelloWorld");
        PyInstance obj = (PyInstance)interpreter.eval("HelloWorld()");
        System.out.println(obj.invoke("get_name").__tojava__(String.class));
    }
}

hello_world1.py

def get_name():
    return "world"

hello_world2.py

class HelloWorld:
    def __init__(self):
        self.world = "world"

    def get_name(self):
        return self.world

【讨论】:

  • 在您的解决方案中如何将参数传递给get_name(self, something)
猜你喜欢
  • 2010-10-02
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2018-06-30
相关资源
最近更新 更多