![](/img/trans.png)
[英]How to call a method from property file using Java and Selenium WebDriver?
[英]How to call method of python file using java
我的python文件的代碼為:
class myPythonClass():
def abc(self):
print "calling abc"
tmpb = {}
tmpb = {'status' : 'SUCCESS'}
return tmpb
我的java代碼是:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/home/Desktop/abc.py");
interpreter.set("myvariable", file1);
PyObject answer = interpreter.eval("myPythonClass().abc(myvariable)");
System.out.println(answer.toString());
在我的python文件中,如果我不給類名加上括號,那么我的java代碼將執行python文件。 但是,如果我的python文件以class myPythonClass():
則它不起作用。 有人可以幫我嗎?
嘗試識別:
class myPythonClass():
def abc(self):
print "calling abc"
tmpb = {}
tmpb = {'status' : 'SUCCESS'}
return tmpb
似乎它沒有縮進,因此它不會成為class
聲明的一部分,Python也不喜歡那樣。 但是我對Java一無所知...
另外,嘗試以注釋( #comment
)開頭,看看是否#comment
。
PS:對不起,如果我不清楚或不確定,我只是無法以自己的聲譽發表評論。
您能否看一下如何從Java類調用Python方法 。
直到您的python代碼正確並正確使用為止。 如果我沒看錯文檔 ,則可以使用eval函數::)
interpreter.execfile("/path/to/python_file.py");
PyDictionary result = interpreter.eval("myPythonClass().abc()");
或者,如果您想獲取字符串:
PyObject str = interpreter.eval("repr(myPythonClass().abc())");
System.out.println(str.toString());
如果要向其提供Java變量的某些輸入,則可以預先使用set,而不要在Python代碼中使用該變量名:
interpreter.set("myvariable", Integer(21));
PyObject answer = interpreter.eval("'the answer is: %s' % (2*myvariable)");
System.out.println(answer.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.