从C#代码激活conda环境(或手动打开cmd与从c#打开cmd有什么区别?) [英] Activating conda environment from c# code (or what is the differences between manually opening cmd and opening it from c#?)

查看:266
本文介绍了从C#代码激活conda环境(或手动打开cmd与从c#打开cmd有什么区别?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用conda环境(dlwin36)在Windows上运行gpu加速的python脚本。

I want to run a gpu accelerated python script on windows using conda environment (dlwin36).

我正在尝试激活dlwin36并执行脚本:

I’m trying to activate dlwin36 and execute a script:

1)激活dlwin36

1) activate dlwin36

2)设置KERAS_BACKEND = tensorflow

2) set KERAS_BACKEND=tensorflow

3)python myscript.py

3) python myscript.py

如果我在计算机上手动打开cmd并输入: activate dlwin36
,则可以正常工作。

If I manually open cmd on my machine and write:"activate dlwin36" it works.

但是当我尝试从c#打开cmd时,我得到:

But when I try opening a cmd from c# I get:

激活未被识别为内部或外部命令,

"activate is not recognized as an internal or external command, operable program or batch file."

命令链接:

var start = new ProcessStartInfo();
start.FileName = "cmd.exe";
start.Arguments = "/c activate dlwin36&&set KERAS_BACKEND=tensorflow&&python myscript.py";
Process.Start(start).WaitForExit();

(我已经测试了UseShellExecute,LoadUserProfile和WorkingDirectory的几种变体)

(I’ve tested several variations of UseShellExecute, LoadUserProfile and WorkingDirectory)

重定向标准输入:

var commandsList = new List<string>();
commandsList.Add("activate dlwin36");
commandsList.Add("set KERAS_BACKEND=tensorflow");
commandsList.Add("python myscript.py");

var start = new ProcessStartInfo();
start.FileName = "cmd.exe";
start.UseShellExecute = false;
start.RedirectStandardInput = true;
var proc = Process.Start(start);
commandsList.ForEach(command => proc.StandardInput.WriteLine(command));

(我已经测试了LoadUserProfile和WorkingDirectory的几种变体)

(I’ve tested several variations of LoadUserProfile and WorkingDirectory)

在两种情况下,我都遇到相同的错误。

In both cases, I got the same error.

似乎手动打开cmd与从c#打开cmd之间存在差异。 p>

It seems that there is a difference between manually opening cmd and opening it from c#.

推荐答案

如果将来可以帮助任何人。我发现您必须从C:\驱动器运行激活。

If this is gonna help anyone in the future. I found that you must run the activation from C:\ drive.

这篇关于从C#代码激活conda环境(或手动打开cmd与从c#打开cmd有什么区别?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆