0

我有一个 python 脚本,我正在使用 Jython 通过 java 进程执行相同的脚本。

数据库——mongodb

Pom.xml

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

Java 进程

public String execute(String val) throws FileNotFoundException,
            ScriptException {
        ClassLoader classLoader = getClass().getClassLoader();
        InputStream is = (InputStream) classLoader
                .getResourceAsStream("my.py");

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(is);

        PyObject someFunc = interpreter.get("myFunc");
        PyObject result = someFunc.__call__(new PyString(val));
        String realResult = (String) result.__tojava__(String.class);
        return realResult;
    }

当我运行 python 脚本时,my.py出现以下错误

File "<iostream>", line 3, in <module>
ImportError: No module named pymongo
4

1 回答 1

0

我通过导入如下模块解决了:-

   PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState());
   PySystemState sys = interpreter.getSystemState();
   sys.path.append(new PyString("\\python_modules\\pymongo-3.3.0-cp26-none-win_amd64.whl"));

我已经从这里下载了pymongo模块。以上对我有用,这样我们可以通过 jython 导入模块。

于 2016-07-31T11:29:08.237 回答