java利用Jython调用python抛出错误ImportError: No module named od

J2EE 码拜 8年前 (2016-05-13) 2775次浏览
Python代码:
import odbchelper
def add(x, y):
“””
add method
“””
return x+y
def sub(x, y):
“””
subtraction method
“””
return x-y
def invokeOdbchelper():
“””
invoking odbchelper.py buildConnectingString()
“””
myParams = {“server”:”mpilgrim”, \
“database”:”master”, \
“uid”:”sa”, \
“pwd”:”secret”
}
return odbchelper.buildConnectionString(myParams)
if __name__ == “__main__”:
print(add(10, 20))
print(sub(20, 19))
print(invokeOdbchelper())
java代码:
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJavaScript
{
public static void main(String args[])
{
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(“E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py”);
PyFunction func = (PyFunction)interpreter.get(“add”,PyFunction.class);
int a = 2010, b = 2 ;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println(“anwser = ” + pyobj.toString());
}//main
}
运行后抛出错误:
Exception in thread “main” Traceback (most recent call last):
File “E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py”, line 4, in <module>
import odbchelper
ImportError: No module named odbchelper
求指导答!!!!! 为什么会抛出如此错误,单独运行Python是正常的。
解决方案

20

Process proc = Runtime.getRuntime().exec(“python  E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py”);
proc.waitFor();

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java利用Jython调用python抛出错误ImportError: No module named od
喜欢 (0)
[1034331897@qq.com]
分享 (0)