2017-04-26 113 views
0

我有一個python模塊,包含import_ical.py__init__.py在我的目錄中。從調用控制檯作品這個模塊,使用時: 蟒蛇-m .import_ical .import_ical.py「TypeError:'module'object is not callable」using Jython

當我打電話用Jython同一模塊,我得到:

TypeError: 'module' object is not callable 
    at org.python.core.Py.TypeError(Py.java:263) 
    at org.python.core.PyObject.__call__(PyObject.java:390) 
    at org.python.core.PyObject.__call__(PyObject.java:496) 
    at services.imports.CalendarImporter.importFromUrl(CalendarImporter.java:53) 
    at services.imports.CalendarImporterTest.testMultipleEventsImport(CalendarImporterTest.java:21) 
    [...] 

CalendarImporter.importFromUrl()執行以下操作:

PythonInterpreter interpreter = new PythonInterpreter(); 
interpreter.exec("import sys"); 
interpreter.exec("sys.path.append('<dir>')"); 
interpreter.exec("sys.path.append('/home/<user>/.local/lib/python2.7/site-packages')"); 
interpreter.exec("import import_ical"); 
PyObject importIcalPy = interpreter.get("import_ical"); 
PyObject pythonResult = importIcalPy.__call__(<parameters go here>); 

當我執行我的JUnit測試(CalendarImporterTest)執行此Jython代碼,在我的模塊目錄中生成一個類文件,命名爲import_ical $ py.class。它包含以下行(其中包括):

@Filename("<dir>/import_ical.py") 
public class import_ical$py extends PyFunctionTable implements PyRunnable { 
    [....] 
    static final PyCode __call__$20; 
    [....] 

    public PyObject __call__$20(PyFrame var1, ThreadState var2) { 
    var1.setline(243); 
    PyObject var3 = var1.getglobal("import_ical").__call__(var2, var1.getlocal(0), var1.getlocal(1), var1.getlocal(2)); 
    var1.f_lasti = -1; 
    return var3; 
    } 
} 

調試上面顯示我的CalendarImporter Java代碼的最後一行給了我以下變量聲明:

interpreter = {PythonInterpreter} 
    [...] 
    globals = {PyStringMap} 
    table 
     [...] 
     1 = {ConcurrentHashMap$MapEntry} "import_ical" -> "<module 'import_ical' from '<dir>/import_ical$py.class'>" 
     [...] 
    [...] 
    [...] 
importIcalPy = {PyModule} 
    [...] 
    __dict__ = {PyStringMap} 
    table 
     [...] 
     19 = {ConcurrentHashMap$MapEntry} "import_ical" -> "<function import_ical at 0xc>" 
     [...] 
     32 = {ConcurrentHashMap$MapEntry} "__call__" -> "<function __call__ at 0x13>" 
     [...] 
    [...] 
    [...] 

爲Python新手,我無法檢測到任何會引起我對模塊生成的類文件的懷疑,甚至上面顯示的變量狀態似乎告訴我,在我的python模塊importIcalPy中有一個合適的函數__call__

注意:我已經將函數__call__添加到我的python模塊中,通過Jython中的「可調用」來創建模塊,並捕獲此錯誤 - 顯然沒有成功。

所以任何人都可以請告訴我:爲什麼我得到那個「不可調用」的錯誤?我能做些什麼來阻止它?任何幫助非常感謝 - 謝謝!

[註釋:我已經強烈地尋找一個解決方案都在#1和使用大搜索引擎,但所有的搜索結果導致我到另一個問題,即一個Python模塊不能調用另一個Python模塊]

+0

「從控制檯調用該模塊時,使用:python -m .import_ical .import_ical.py」 - 這不會調用模塊。模塊不可調用。如果您希望模塊對象實現'__call__'等同於'python -m thatmodule',否,那不是模塊的工作方式。 – user2357112

+0

@ user2357112感謝您的澄清。正如我所指出的,我手動添加了'__call__'來使模塊可以被Java調用,而之前它已經使用'python -m'正常執行。我稱之爲「調用這個模塊」的非正式表達是由錯誤信息產生的(參見問題標題)。無論如何,這並不能幫助我解決這個問題,是嗎? – philburns

+1

在模塊中放置__call__函數不會使其可調用。它會使Python屬性訪問'import_ical .__ call__'成功,但Java端'__call__'方法是一種不同的方法,它將忽略你的函數。 (如果你嘗試類似'import_ical()'的東西,Python方面也會忽略你的'__call__'函數。) – user2357112

回答

0

最後,感謝user2357112的提示,我找到了一個解決方案。更換CalendarImporter.importFromUrl的內容()用下面的代碼上面顯示:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]); 
PythonInterpreter interpreter = new PythonInterpreter(); 
interpreter.exec("import sys"); 
interpreter.exec("sys.path.append('<dir>')"); 
interpreter.exec("sys.path.append('/home/<user>/.local/lib/python2.7/site-packages')"); 
interpreter.exec("import import_ical"); 
// set variables if necessary in script: 
// interpreter.set("__file__", <myFile>); 
// set system argument variables (one append per variable): 
interpreter.exec("sys.argv.append('".concat("<myVar>").concat("')")); 
interpreter.execfile("<fileWithQualifiedPath>"); 
PyObject importIcalPy = interpreter.get("import_ical"); 
PyObject pythonResult = importIcalPy.__call__(new PyString("<myScriptArgument>")); 

希望,這將幫助別人。

相關問題