- windows环境
windows环境下调用python虚拟环境conda报错: importerror: dll load failed while importing _gdal: 找不到指定的模块。
解决:
- 编写activate.cmd
:: windows切换环境 activate gdal3.3.1
- java调用脚本
// 先切换环境再执行脚本 String cmd = "D:\\ars\\m2m\\activate.cmd && " ; // 切换环境 += " python D:\\ars\\m2m\\tvdi.py" ; // 执行脚本 += " --LC08_fn --shape_fn" ; // 传递参数 logger.info("-------------------> " + cmd + " <-------------------"); // 执行 Process p = Runtime.getRuntime().exec(cmd); String read, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(), "gbk")); while ((read = in .readLine()) != null) { result += read; } p.waitFor(); // 返回结果 logger.info("-------------------> " + result + " <-------------------");
- linux环境
# 查找 whereis conda # 查找 which python > /root/miniconda3/envs/mmenv/bin/python
- 全路径执行
@Test void testDk() throws IOException, InterruptedException { String cmd = "/root/miniconda3/envs/mmenv/bin/python ~/0predict1.py"; Process p = Runtime.getRuntime().exec(cmd); String read, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(), "gbk")); while ((read = in.readLine()) != null) { result += read; } p.waitFor(); logger.info("-------------------> result: " + result + " <-------------------"); }