首页 > 解决方案 > 从 Java jar 调用时,Jython 代码抛出 ImportError

问题描述

我正在尝试从捆绑为 JAR 的 Spring Boot 应用程序调用 Python 脚本。我为Jython Standalone 2.7.1添加了 Maven 依赖项。以下代码在execfile调用中引发错误(代码能够正确获取 Python 文件作为输入流):

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
String[] result = new String[2];
Properties preprops = System.getProperties();

Properties props = new Properties();
//props.put("python.home", "c:/Python27");
//props.put("python.path", "c:/Python27/Lib");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false"); 
props.put("python.import.site", "false");

PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter pyInterpreter = new PythonInterpreter();

try {
    Resource resource = new ClassPathResource("myPythonFile.py");
    pyInterpreter.set("args", "a string argument");
    pyInterpreter.setOut(out);
    pyInterpreter.setErr(err);
    pyInterpreter.execfile(resource.getInputStream());

    result[0] = out.toString(); // reading the output
    result[1] = err.toString(); // reading the error
} catch (Exception e) {
    logger.error("Error in code: ", e);
} finally {
    try {
        if (out != null)
            out.close();
        if (err != null)
            err.close();
        pyInterpreter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

抛出的错误:

org.python.core.PyException: null
        at org.python.core.Py.ImportError(Py.java:334) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.imp.import_first(imp.java:879) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.imp.import_module_level(imp.java:964) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.imp.importName(imp.java:1057) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.PyObject.__call__(PyObject.java:450) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.__builtin__.__import__(__builtin__.java:1232) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.imp.importOne(imp.java:1076) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.pycode._pyx1.f$0(<iostream>:251) ~[na:na]
        at org.python.pycode._pyx1.call_function(<iostream>) ~[na:na]
        at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.PyCode.call(PyCode.java:18) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.core.Py.runCode(Py.java:1614) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:296) ~[jython-standalone-2.7.1.jar!/:na]
        at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:291) ~[jython-standalone-2.7.1.jar!/:na]

我的 Python 脚本使用以下导入:

import sys
import argparse
import re
import json

我认为这可能正在发生,因为我无法设置属性“ python.home ”或“ python.path ”,但我不确定我该怎么做(Jython JAR 捆绑在BOOT-INF/lib下通过 Spring Boot)。如果我将“ python.home ”或“ python.path ”属性设置为我的本地 Python 安装路径,代码就可以正常工作。

感谢您帮助解决这个问题。https://github.com/dchucks/jython提供了一个示例应用程序,以帮助重现该错误。

标签: javapython-3.xspring-bootjython-2.7

解决方案


推荐阅读