- 使用Java调用python时,python文件内如有读写文件的操作,其路径必须为绝对路径。如:使用pandas读取csv文件时如果使用相对路径则程序将无法正常执行。
- 使用Java调用python时,python文件内如果导入自定义的其他python脚本,则无法正常执行,需将其打包成第三方库并安装至python环境中方可正常使用。
- 推荐调用python方式如下:
public class execPython { public static void main(String[] args) { Process proc; try { proc = Runtime.getRuntime().exec("python D:\\PyCharm\\Projects\\Test\\dlib\\Dlib_face_recognition_from_camera-master\\face_reco_from_camera.py");// 执行py文件 //用输入输出流来截取结果 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }
python项目打包为库,参考:https://blog.csdn.net/qq_34673805/article/details/99634250