虽然conda init已经运行,但无法使用subprocess.run enventheless运行conda命令。

问题描述 投票:0回答:1

正如这个片段所示。conda init 已被运行,但conda命令却以 subprocess.run() 不同意。为什么呢?

In [9]: import subprocess                                                                                                     

In [10]: subprocess.run("conda activate test", shell=True)                                                                    

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


Out[10]: CompletedProcess(args='conda activate test', returncode=1)

In [11]: !echo $SHELL                                                                                                         
/usr/bin/zsh

In [12]: !conda init zsh                                                                                                      
no change     /home/miniconda3/condabin/conda
no change     /home/miniconda3/bin/conda
no change     /home/miniconda3/bin/conda-env
no change     /home/miniconda3/bin/activate
no change     /home/miniconda3/bin/deactivate
etc.

No action taken.

编辑:cel给出的前缀解决方案似乎在这里不起作用。

In [2]: subprocess.run("$CONDA_PREFIX/etc/profile.d/conda.sh && conda activate test", shell=True)                             

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


Out[2]: CompletedProcess(args='$CONDA_PREFIX/etc/profile.d/conda.sh && conda activate test', returncode=1)
python shell subprocess conda
1个回答
2
投票

最后,解决方案是(在做了 $CONDA_PREFIX/etc/profile.d/conda.sh 可执行 chmod +x $CONDA_PREFIX/etc/profile.d/conda.sh我不得不用 . 而不是 source):

subprocess.run(". $CONDA_PREFIX/etc/profile.d/conda.sh && conda activate test", shell=True)

这里详细讨论了这个问题。https:/github.comcondacondaissues7980。

© www.soinside.com 2019 - 2024. All rights reserved.