Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 597
Omri Avatar asked Mar 03 '18 08:03

Omri


2 Answers

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);
}
like image 121
gls123 Avatar answered Oct 06 '22 00:10

gls123


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";
like image 41
mhdbny Avatar answered Oct 06 '22 00:10

mhdbny