从 C# 代码激活 conda 环境(或者手动打开 cmd 和从 C# 打开它有什么区别?)

Activating conda environment from c# code (or what is the differences between manually opening cmd and opening it from c#?)

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

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

1) 激活dlwin36

2) 设置 KERAS_BACKEND=tensorflow

3) python myscript.py

如果我在我的机器上手动打开 cmd 并写入:"activate dlwin36" 它有效。

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

“activate 不是内部或外部命令,也不是可运行的程序或批处理文件。”

我试过使用以下方法:

命令链:

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 的几种变体)

重定向标准输入:

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 的几种变体)

在这两种情况下,我都得到了同样的错误。

手动打开cmd和c#打开好像有区别

您需要使用您环境中的 python.exe。例如:

Process proc = new Process();
proc.StartInfo.FileName = @"C:\path-to-Anaconda3\envs\tensorflow-gpu\python.exe";

或者您的情况:

start.Arguments = "/c activate dlwin36&&set KERAS_BACKEND=tensorflow&&\"path-to-Anaconda3\envs\tensorflow-gpu\python.exe\" myscript.py";

如果这对以后的任何人都有帮助。我发现您必须 运行 从 C:\ 驱动器激活。

关键是在你的 cmd.exe 中 运行 activate.bat,然后再做任何其他事情。

// Set working directory and create process
var workingDirectory = Path.GetFullPath("Scripts");
var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        RedirectStandardInput = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        WorkingDirectory = workingDirectory
    }
};
process.Start();
// Pass multiple commands to cmd.exe
using (var sw = process.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
        // Vital to activate Anaconda
        sw.WriteLine("C:\PathToAnaconda\anaconda3\Scripts\activate.bat");
        // Activate your environment
        sw.WriteLine("activate your-environment");
        // Any other commands you want to run
        sw.WriteLine("set KERAS_BACKEND=tensorflow");
        // run your script. You can also pass in arguments
        sw.WriteLine("python YourScript.py");
    }
}

// read multiple output lines
while (!process.StandardOutput.EndOfStream)
{
    var line = process.StandardOutput.ReadLine();
    Console.WriteLine(line);
}

我花了一些时间在这上面,这是唯一对我有用的东西:运行 一个批处理文件,它将激活 conda 环境,然后在 python 中发出命令,像这样。我们称之为 run_script.bat:

call C:\Path-to-Anaconda\Scripts\activate.bat myenv
set KERAS_BACKEND=tensorflow
python YourScript.py
exit

(注意在我们调用激活批处理文件之前使用 call 关键字。)

之后您可以从 C# 或多或少地 运行 它,如上所示。

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "cmd.exe";
start.Arguments = "/K c:\path_to_batch\run_script.bat";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.WorkingDirectory = "c:\path_to_batch";
string stdout, stderr;
using (Process process = Process.Start(start))
{
    using (StreamReader reader = process.StandardOutput)
    {
        stdout = reader.ReadToEnd();
    }

    using (StreamReader reader = process.StandardError)
    {
        stderr = reader.ReadToEnd();
    }

    process.WaitForExit();
}

我正在用 C# 即时生成批处理文件以设置必要的参数。