"使用Jython时出现'typeerror:'模块'对象不可调用"

2024-09-28 21:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python模块,由目录中的import_ical.py和{}组成。使用以下命令时,从控制台调用此模块可以正常工作: python-m.import\ulic.import_日历.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()执行以下操作:

^{pr2}$

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

@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;
  }
}

调试到上面所示的CalendarImporterJava代码的最后一行,可以得到以下变量状态:

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“调用”该模块并捕获此错误—显然没有成功。在

那么有谁能告诉我:为什么我会得到“不可调用”的错误?我能做些什么来阻止它呢?非常感谢您的帮助-谢谢!在

[注释:我在Stackoverflow和使用一个大型搜索引擎的情况下都强烈地搜索了一个解决方案,但是所有的搜索结果都会导致另一个问题,即python模块无法调用另一个python模块。]


Tags: 模块pyorgcoreimportjythonjavacall
1条回答
网友
1楼 · 发布于 2024-09-28 21:52:54

最后,多亏了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>"));

希望,它会帮助别人。在

相关问题 更多 >