Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python脚本中激活Conda环境?_Python_Conda_Environment - Fatal编程技术网

如何在Python脚本中激活Conda环境?

如何在Python脚本中激活Conda环境?,python,conda,environment,Python,Conda,Environment,我有一个使用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

我有一个使用
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'.
CommandNotFoundError:您的shell未正确配置为使用“conda activate”。
要初始化shell,请运行
$conda init
当前支持的shell有:
-猛击
-鱼
-tcsh
-克逊什
-zsh
-动力壳
有关更多信息和选项,请参阅“conda init--help”。
重要提示:在运行“conda init”后,您可能需要关闭并重新启动shell。
事实是,我的shell配置正确。如果我在shell中运行相同的
cmd
,一切都会正常工作

我也尝试过使用
subprocess.run(cmd,shell=True)
,但得到了相同的结果


有没有一种方法可以从python脚本中运行
conda activate base

conda activate
实际上是用于交互式设置(shell/命令提示)。但是您可以使用
conda run
在给定环境中执行特定的python脚本

请尝试以下方法:

conda run -n <environment> <script_name>
conda运行-n

如果这是macOS/linux,您需要
source/etc/profile.d/conda.sh
才能运行conda命令。@cel。你能把它作为一个答案发布吗?我得到了
拒绝的许可
我不能重现那个错误,但是听起来好像你已经有了@cel发布的解决方案