zoukankan      html  css  js  c++  java
  • 关于在bash script中无法conda activate env的解决办法

    1. 场景

    使用anaconda环境进行开发是一件很方便的事情,然后到了开发完成,需要部署环境的时候,我们一般会将项目进程的启停写入自动化脚本进行管理,这个时候如果直接在脚本中激活环境,往往会给你一个警告:

    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是bash,你直接在shell中运行 conda init bash,然后发现还是没解决问题,只是在~/.bashrc 文件的最后加多了类似以下的内容:

    # >>> conda initialize >>>
    # !! Contents within this block are managed by 'conda init' !!
    __conda_setup="$('/home/ubuntu/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
         eval "$__conda_setup"
    else
         if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
             . "/home/ubuntu/anaconda3/etc/profile.d/conda.sh"
         else
             export PATH="/home/ubuntu/anaconda3/bin:$PATH"
         fi
    fi
    unset __conda_setup
    # <<< conda initialize <<<

    没错,这就是conda的环境设置,而我们执行bash script的时候,一般是fork一个子进程,并不会去读conda的设置,因此我们需要在脚本中手动设置一下。


    2. 解决

    在运行的脚本中,conda执行之前,加入以下内容:

    if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/ubuntu/anaconda3/etc/profile.d/conda.sh"
     else
         export PATH="/home/ubuntu/anaconda3/bin:$PATH"
     fi

    具体的路径以实际情况为主,然后重新执行。


    3. 参考

    (完)

  • 相关阅读:
    P4097 [HEOI2013]Segment(李超线段树模板)
    P2155 [SDOI2008]沙拉公主的困惑
    BZOJ3675 [Apio2014]序列分割[斜率优化dp]
    hdu4261 Estimation[暴力dp+对顶堆]
    poj2374 Fence Obstacle Course[线段树+DP]
    poj1463 Strategic game[树形DP]
    CH5E02 [IOI1999]花店橱窗[暴力dp]
    CH5E01[NOIP2010] 乌龟棋[暴力]
    CH5702 Count The Repetitions[倍增dp]
    P1081 [NOIP2012]开车旅行[倍增]
  • 原文地址:https://www.cnblogs.com/harrymore/p/13410267.html
Copyright © 2011-2022 走看看