如何在 Python 脚本中激活 Conda 环境?
How to activate a Conda environment within a Python script?
我有一个使用 os.system(cmd)
到 运行 管道的脚本。我需要在特定的 Conda 环境中连接到 运行,所以我尝试这样做:
cmd = 'conda activate base && ' + cmd
os.system(cmd)
但是,我得到:
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'.
事实上,我的 shell 配置正确。如果我 运行 在 shell 中使用相同的 cmd
,一切都按预期工作。
我也试过使用 subprocess.run(cmd, shell=True)
,但我得到了相同的结果。
有没有办法从 python 脚本中 运行 conda activate base
?
conda activate
真正用于交互设置(shells/command 提示)。但是您可以使用 conda run
在给定环境中执行特定的 python 脚本。
试试这个:
conda run -n <environment> <script_name>
我有一个使用 os.system(cmd)
到 运行 管道的脚本。我需要在特定的 Conda 环境中连接到 运行,所以我尝试这样做:
cmd = 'conda activate base && ' + cmd
os.system(cmd)
但是,我得到:
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'.
事实上,我的 shell 配置正确。如果我 运行 在 shell 中使用相同的 cmd
,一切都按预期工作。
我也试过使用 subprocess.run(cmd, shell=True)
,但我得到了相同的结果。
有没有办法从 python 脚本中 运行 conda activate base
?
conda activate
真正用于交互设置(shells/command 提示)。但是您可以使用 conda run
在给定环境中执行特定的 python 脚本。
试试这个:
conda run -n <environment> <script_name>