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

5

7

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

Messuage answered 3/3, 2018 at 8:40 Comment(0)
M
-2

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

Messuage answered 17/3, 2018 at 16:27 Comment(0)
L
15

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);
}
Lexicostatistics answered 10/5, 2018 at 12:15 Comment(1)
Thanks a lot! you can find full command Anacaonda is using in it's command prompt by right clicking on Anaconda prompt and going to properties in the target section: %windir%\System32\cmd.exe "/K" C:\Users\na\AppData\Local\Continuum\anaconda3\Scripts\activate.bat C:\Users\na\AppData\Local\Continuum\anaconda3Willette
S
1

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";
Saccharify answered 17/3, 2018 at 15:14 Comment(0)
J
1

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.

Judsonjudus answered 3/12, 2018 at 2:26 Comment(0)
R
0

Maybe this helps:

  1. Set Conda environment variables according to yours,like: myProcessStartInfo.EnvironmentVariables["CONDA_PREFIX"] = "c:\.conda\myenv"

  2. Run this in cmd-prop (where conda env. is active) : where python ,to make sure that python.exe is run in correct conda environment and change ProcessStartInfo Filename to match that output, like:

    var start = new ProcessStartInfo { FileName = @"C:\Users\sami\.conda\envs\myenv\python.exe",

Rhombohedron answered 7/6, 2024 at 11:47 Comment(0)
M
-2

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

Messuage answered 17/3, 2018 at 16:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.