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

goto_217 2014-03-20 09:20:50
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是正常的。。
...全文
2078 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ehcoing 2016-08-05
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
Process proc = Runtime.getRuntime().exec("python E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py"); proc.waitFor();
用这个proc怎么调用python里的add方法呢
我想当好人 2016-07-26
  • 打赏
  • 举报
回复
楼主最后的解决方法 在我这里也没有用呢
jethuang007 2014-09-03
  • 打赏
  • 举报
回复
用你的方法试了,还是没反应,错误提示:File<String>,line 1,in <module> ImportError:No module named psycopg2
goto_217 2014-03-20
  • 打赏
  • 举报
回复
问题已经解决,,python中导入的库需要在java中提前导入一下,具体代码如下: import org.python.core.Py; import org.python.core.PyFunction; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; public class FirstJavaScript { public static void main(String args[]) { PythonInterpreter interpreter = new PythonInterpreter(); //‘odbchelper’是自定义的python库,在 Python 方法中我们需要用到这个库中的方法,所以需要提前导入进来 PySystemState sys = Py.getSystemState(); sys.path.add("E:\\_Python\\diveintopythonzh-cn-5.4b\\py"); interpreter.exec("import odbchelper"); 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 }
tony4geek 2014-03-20
  • 打赏
  • 举报
回复
Process proc = Runtime.getRuntime().exec("python E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py"); proc.waitFor();

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧