Java运⾏python程序(基于conda环境下的pytorch库)1、⾸先确定python程序的路径pathPython
2、确定python 的scripts的路径找到activate.bat,在此路径下cmd,⽤于找到conda的基础环境(ba)
蒋欣书法家3、定位到含有.conda⽂件下的C盘(我的是C:\Urs\pc)
4、此时已经是conda的环境下了,再激活对应的pytorch库
5、运⾏python 程序
6、代码解析
晋升申请书范文
主要是Process process = Runtime().exec("…………………………………………");代码来实现
省略号,是cmd Windows 命令提⽰符内要⾛的过程,每步⽤&&隔开,如下:
condaPath+"\\activate.bat && C: && conda activate pytorch && python "+path+"donghua_FDD_2.py"
其他部分的代码⽤于接收数据、接收python控制台的内容(下⾯那些Loss),可根据需要删减
String path ="D:\\python_dcgan\\python1\\dcgan\\";//获取到项⽬⽬录后,补充完整python⽂件所在⽬录
个性网名path =place("\\","\\\\");
String condaPath = "D:\\python\\Scripts";//获取conda的系统变量
System.out.println(condaPath);
Process process = Runtime().exec(condaPath+"\\activate.bat && C: && conda activate 让爱飞扬
pytorch && python "+path+"donghua_FDD_2.py"); int re=process.waitFor();//re=0成功,re=1失败
BufferedReader in = new BufferedReader(new InputStream()));
in = new BufferedReader(new InputStream(),"gbk"));
//接收错误流秋天作文200字
BufferedReader isError = new BufferedReader(new ErrorStream(),"gbk"));
StringBuilder sb= new StringBuilder();
StringBuilder sbError= new StringBuilder();
卢浮魅影女主角String line=null;
String lineError= null;
process.waitFor();
while ((line = in.readLine()) != null) {
sb.append(line);
怎么贴双眼皮贴sb.append("\n");素描花朵
}
System.out.println(sb);
while ((lineError= adLine()) != null) {
sbError.append(lineError);
sbError.append("\n");
}
System.out.println(sbError);
in.clo();
isError.clo();
String();