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

半腔热情 提交于 2020-02-21 23:57:02

问题


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

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

1) activate dlwin36

2) set KERAS_BACKEND=tensorflow

3) python myscript.py

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

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.”

I tried using the following methods:

Command chaining:

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

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

Redirect standard input:

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));

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

In both cases, I got the same error.

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


回答1:


The key is to run activate.bat in your cmd.exe before doing anything else.

// 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);
}



回答2:


You need to use the python.exe from your environment. For example:

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

or in your case:

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



回答3:


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




回答4:


I spent a bit of time working on this and here's the only thing that works for me: run a batch file that will activate the conda environment and then issue the commands in python, like so. Let's call this run_script.bat:

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

(Note the use of the call keyword before we invoke the activate batch file.)

After that you can run it from C# more or less as shown above.

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();
}

I am generating the batch file on the fly in C# to set the necessary parameters.



来源:https://stackoverflow.com/questions/49082312/activating-conda-environment-from-c-sharp-code-or-what-is-the-differences-betwe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!