无法从子进程激活Conda环境

Bin*_*kar 6 python conda electron

我正在构建一个Electron应用程序(运行一个 Angular 应用程序),它充当python底层程序的用户界面。

python程序用于anaconda包管理(我正在使用miniconda开发)。

当应用程序启动时,它会检查所需的conda环境是否存在,如果不存在,则创建它。

以下代码是Service负责管理python程序的一部分。

public doEnvironmentSetup() {
    let stdOutSub = new Subject<string>();
    let stdErrSub = new Subject<string>();
    let completeSubject = new Subject<string>();
    this.runningSetup = true;

    const TEMP_ENV_FILE = join(tmpdir(), "env.yml");

    return Promise.resolve()
      .then(() => {

        // Copy packaged environment.yml to TEMP_ENV_FILE

      })
      .then(() => this.electron.getApplicationStoragePath())
      .then((stor) => {

        setTimeout(() => {

          let runProcess = this.electron.childProcess.spawn("conda", ["env", "create", "--file", TEMP_ENV_FILE, "--name", CONDA_ENV_NAME], {
            cwd: stor
          });

          const stdOutReaderInterface = createInterface(runProcess.stdout);
          const stdErrReaderInterface = createInterface(runProcess.stderr);

          stdOutReaderInterface.on('line', (line) => {
            stdOutSub.next(line);
          });

          stdErrReaderInterface.on('line', (line) => {
            stdErrSub.next(line);
          });

          runProcess.on('close', (code: number) => {
            this.electron.fs.unlinkSync(TEMP_ENV_FILE);
            this.runningSetup = false;
            completeSubject.next("");
          });

        }, 2000);

        return {
          stdOut: stdOutSub,
          stdErr: stdErrSub,
          onComplete: completeSubject
        };

      });

  }
Run Code Online (Sandbox Code Playgroud)

现在,当我需要运行实际python代码时,运行的代码片段是(没有给出整个代码,因为它对于我们的目的来说太长了):

        execCmd.push(
          `conda init ${this.electron.os.platform() === "win32" ? "powershell" : "bash"}`,
          `conda activate ${CONDA_ENV_NAME}`,
          // long python spawn command
          `conda deactivate`,
        )

        setTimeout(() => {

          logLineSubject.next({ out: "--- Setting up Execution Environment ---", err: "" });

          logLineSubject.next({ out: `Running in ${dir}`, err: "" });

          const cmd = execCmd.join(" && ");

          let runProcess = this.electron.childProcess.spawn(cmd, {
            detached: false,
            windowsHide: true,
            cwd: cwd,
            shell: this.getShell()
          });

          const stdOutInterface = createInterface(runProcess.stdout);
          const stdErrInterface = createInterface(runProcess.stderr);

          stdOutInterface.on('line', (line) => {
            // get this line back to the component
          });

          stdErrInterface.on('line', (line) => {
            // get this line back to the component
          });

          runProcess.on("error", (err) => {
            // get this back to the component
          });

          runProcess.on('close', (code: number) => {
            // get this line back to the component
          });

        }, 1000);
Run Code Online (Sandbox Code Playgroud)

其中getShell定义为:

private getShell() {
  return process.env[this.electron.os.platform() === "win32" ? "COMSPEC" : "SHELL"];
}

Run Code Online (Sandbox Code Playgroud)

然而,每当我尝试运行它时,它都会返回:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
    $ conda init <SHELL_NAME>
blah blah blah ...
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 时source activate ${CONDA_ENV_NAME},它会返回:

/bin/bash: activate: No such file or directory
Run Code Online (Sandbox Code Playgroud)

不太确定我在这里做错了什么。有人可以指出我正确的方向吗?

PS:它可以与 一起使用source $(conda info --root)/etc/profile.d/conda.sh,但我不能真正使用它,因为我还需要支持 Windows!

python免责声明:我是/的新手anaconda

mer*_*erv 4

我不确定需要在 Windows Powershell 中运行什么,但对于 bash,您需要运行将conda initbash 配置为在启动时运行的脚本,而不是conda init. 那是,

miniconda3/etc/profile.d/conda.sh
Run Code Online (Sandbox Code Playgroud)

所以它应该是这样的

execCmd.push(
    `. ${CONDA_ROOT}/etc/profile.d/conda.sh`,
    `conda activate ${CONDA_ENV_NAME}`,
    // long python spawn command
     `conda deactivate`,
)
Run Code Online (Sandbox Code Playgroud)

我怀疑 Windows 案例正在运行目录中的 .bat 或 .ps1 文件之一condabin

或者,如果conda已定义并且您有一个 Python 脚本(例如script.py),那么您可能可以不用使用conda run,例如

execCmd.push(
    `conda run -n ${CONDA_ENV_NAME} python script.py`
)
Run Code Online (Sandbox Code Playgroud)

这可能可以跨平台工作。请注意,conda run最近才添加了对交互式 I/O 的支持,并且必须使用--live-stream标志启用它(请参阅v4.9.0 发行说明)。否则,它只是缓冲所有命中 stdout/stderr 的内容,并且在进程退出之前不会返回它。