1 回答

TA贡献1816条经验 获得超4个赞
根据该问题上述评论部分的建议,我已经为我的问题制定了解决方案。以下代码段将证明这一点。在这个解决方案中,我将python.path设置为我的模块文件的目录路径。
public static void main(String[] args) throws PyException{
Properties properties = new Properties();
properties.setProperty("python.path", "/path/to/the/module/directory");
PythonInterpreter.initialize(System.getProperties(), properties, new String[]{""});
PythonInterpreter pi = new PythonInterpreter();
pi.exec("from JythonTestModule import square");
pi.set("integer", new PyInteger(42));
pi.exec("result = square(integer)");
pi.exec("print(result)");
PyInteger result = (PyInteger)pi.get("result");
System.out.println("result: "+ result.asInt());
PyFunction pf = (PyFunction)pi.get("square");
System.out.println(pf.__call__(new PyInteger(5)));
}
如果要使用 Jython 中的多个模块,请将python.path添加为所有模块的父目录路径,以便检测所有模块。
添加回答
举报