Java &引用;类型错误:';模块';对象不可调用";使用Jython

Java &引用;类型错误:';模块';对象不可调用";使用Jython,java,python,module,jython,callable,Java,Python,Module,Jython,Callable,我有一个python模块,由我的目录中的import_icial.py和\uuu init\uuu.py组成。使用以下命令时,从控制台调用此模块有效: python-m.import_icial.import_icial.py 当我使用Jython调用同一个模块时,我得到: TypeError: 'module' object is not callable at org.python.core.Py.TypeError(Py.java:263) at org.python.core.P

我有一个python模块,由我的目录中的
import_icial.py
\uuu init\uuu.py
组成。使用以下命令时,从控制台调用此模块有效: python-m.import_icial.import_icial.py

当我使用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>);
调试到我的
日历导入程序的最后一行
上面显示的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>"
      [...]
    [...]
  [...]
解释器={PythonInterpreter}
[...]
globals={PyStringMap}
桌子
[...]
1={ConcurrentHashMap$MapEntry}“导入”->“
[...]
[...]
[...]
importIcalPy={PyModule}
[...]
__dict_uuu={PyStringMap}
桌子
[...]
19={ConcurrentHashMap$MapEntry}“导入”->“
[...]
32={ConcurrentHashMap$MapEntry}“\uuuuuu调用\uuuuuuuuu”->”
[...]
[...]
[...]
作为一名python新手,我无法检测到任何会引起我对模块生成的类文件的怀疑的东西,甚至上面显示的变量状态似乎也告诉我,在我的python模块
importIcalPy
中有一个适当的函数
\uu调用

注意:我已经将函数
\uuuuu call\uuuu
添加到我的python模块中,通过Jython的“callable”使模块成为可调用的,并捕获此错误-显然没有成功

所以,谁能告诉我:为什么我会出现“不可调用”错误?我能做些什么来防止它呢?非常感谢您的帮助-谢谢


[备注:我在Stackoverflow和使用大型搜索引擎两方面都进行了大量搜索,但所有搜索结果都导致了另一个问题,即python模块无法调用另一个python模块。]

最后,多亏了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>"));
PythonInterpreter.initialize(System.getProperties(),System.getProperties(),新字符串[0]);
PythonInterpreter解释器=新的PythonInterpreter();
explorer.exec(“导入系统”);
explorer.exec(“sys.path.append(“”)”;
explorer.exec(“sys.path.append('/home/.local/lib/python2.7/site packages');
explorer.exec(“导入”);
//如有必要,在脚本中设置变量:
//set(“_文件__”);
//设置系统参数变量(每个变量一个附加):
explorer.exec(“sys.argv.append(”).concat(“”).concat(“”));
explorer.execfile(“”);
PyObject importIcalPy=解释器.get(“导入”);
PyObject pythonResult=importIcalPy.\uuuu调用(新PyString(“”);

希望,它能帮助一些人。

“在使用:python-m.import_-ical.import_-ical.py时,从控制台调用此模块是有效的”-这不是调用模块。模块不可调用。如果您希望模块对象实现与python-m thatmodule相当的
\uuu调用
,则不,模块不是这样工作的。@user2357112感谢您的澄清。正如我所指出的,我手动添加了
\uuu call\uu
以使模块可由Java调用,而之前它已经使用
python-m
很好地执行了。我的不精确表达式“调用此模块”是由错误消息引起的(请参阅问题标题)。无论如何,这并不能帮助我解决这个问题,是吗?在模块中放置一个
\uuuu call\uuu
函数并不能使其可调用。它将使Python属性access
import\icial.\uuuuu调用\uuuu
成功,但Java端的
\uuuu调用\uuuu
方法是另一种忽略函数的方法。(如果您尝试了类似于
import_icial()
)的操作,Python端也会忽略您的
\uuu调用
函数)@user2357112再次感谢!有趣-我没有看到这些是不同的调用函数。所以,回到错误消息:“TypeError:‘module’对象不可调用”:原因可能是什么?模块对象不可调用。您正在尝试调用模块对象。这很清楚。
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>"));