Search code examples
c#pythonwindowscmdconda

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


Solution

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