"TypeError: 'module' object is not callable" using Jython

"TypeError: 'module' object is not callable" using Jython

我的目录中有一个 python 模块,由 import_ical.py__init__.py 组成。使用时从控制台调用此模块有效: python -m .import_ical .import_ical.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>);

当我执行执行此 Jython 代码的 JUnit 测试 (CalendarImporterTest) 时,在我的模块目录中生成了一个 class 文件,名为 import_ical$ py.class。它包含以下几行(除其他外):

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

  public PyObject __call__(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 新手,对于模块生成的 class 文件,我无法检测到任何会引起我怀疑的东西,甚至上面显示的变量状态似乎也告诉我在我的 python 模块 importIcalPy.

中有一个合适的函数 __call__

注意:我已经将函数 __call__ 添加到我的 python 模块以通过 Jython "callable" 创建模块并捕获此错误 - 显然没有成功。

那么谁能告诉我:为什么我会收到 "not callable" 错误?我能做些什么来防止它?非常感谢任何帮助 - 谢谢!

[评论:我在 Whosebug 和使用大型搜索引擎中都在努力寻找解决方案,但所有搜索结果都将我引向另一个问题,其中 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>"));

希望对某人有所帮助。