1

我正在将 Spring Boot 用于 Java 应用程序,并且我想在应用程序中放置一个 python 模块my_module.py。我正在尝试导入模块,例如

interpretor.exec("import my_impodule")

但是我收到了错误ImportError: No Module named my_module,当我使用检查当前工作目录时

interpretor.exec("import os\nprint os.getcwd()")

这给了我路径/my_project/,我的模块位置/my_project/my_module.py是正确的。如果当前工作目录是这个,它应该选择模块。

有人可以帮我把python模块放在哪里,以便我可以被Jython接走。

4

1 回答 1

1

您需要设置 Python 模块路径。这样它就可以像这样选择您的模块:

Properties pyProperties = new Properties();
pyProperties.put("python.path", System.getProperty("user.dir") + MODULE_PATH);
PythonInterpreter.initialize(System.getProperties(), pyProperties(), new String[0]);
PythonInterpreter pyInterpreter = new PythonInterpreter();
于 2016-04-28T06:27:00.403 回答