美文网首页
解决AWS AMI机器学习服务器conda环境激活问题

解决AWS AMI机器学习服务器conda环境激活问题

作者: Jack_Woo | 来源:发表于2019-06-20 16:02 被阅读0次

    作者:Jack Wu

    现象

    AWS Ubuntu AMI机器学习服务器无法自动激动 conda 的 tensorflow 环境问题。如我们想自动活动tensorflow_p36,在conda的提示下,于是在.bashrc中追加如下配置:

    . /home/ubuntu/anaconda3/etc/profile.d/conda.sh
    conda activate tensorflow_p36
    

    但登录后发现使用的Python仍然是conda base的Python:

    (tensorflow_p36) ubuntu@ip:~$ which python
    which python
    /home/ubuntu/anaconda3/bin/python
    

    原因

    当登录或tmux或bash嵌套时,脚本调用顺序.profile -> .bashrc。而.profile的内容如下。

    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    # see /usr/share/doc/bash/examples/startup-files for examples.
    # the files are located in the bash-doc package.
    
    # the default umask is set in /etc/profile; for setting the umask
    # for ssh logins, install and configure the libpam-umask package.
    #umask 022
    
    # if running bash
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
            . "$HOME/.bashrc"
        fi
    fi
    # for private env
    PATH="$HOME/bin:$HOME/.local/bin:$PATH"
    export PATH="/home/ubuntu/anaconda3/bin:$PATH"
    

    可以发现,其最后两行的环境变量配置,覆盖了conda activate tensorflow_p36生成的环境变量,通过env查看PATH可看到,

    PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/anaconda3/bin/:/home/ubuntu/anaconda3/envs/tensorflow_p36/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/anaconda3/bin/:/usr/local/cuda/bin:/usr/local/bin ...
    

    而这时我们已经在tensorflow_p36环境中,再运行conda activate tensorflow_p36也无效果。临时的解决方法是先deactivate再激活tensorflow_p36,以再覆盖一次环境变量PATH。

    conda deactivate
    conda activate tensorflow_p36
    
    PATH=/home/ubuntu/anaconda3/envs/tensorflow_p36/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/anaconda3/bin/:/home/ubuntu/anaconda3/envs/tensorflow_p36/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/anaconda3/bin/:/usr/local/cuda/bin:/usr/local/bin ...
    

    如何分析出如上的原因呢,可以在.bashrc头加上set -x,使bash打印每次运行的脚本的第一行信息:

    #Source .dlamirc in .bashrc
    set -x
    source ~/.dlamirc
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    ...
    

    解决

    从上面可以看出,比较偷懒的解决方法是,修改.bashrc中的 tensorflow_p36 激活脚本为如下:

    . /home/ubuntu/anaconda3/etc/profile.d/conda.sh
    conda deactivate
    conda activate tensorflow_p36
    

    相关文章

      网友评论

          本文标题:解决AWS AMI机器学习服务器conda环境激活问题

          本文链接:https://www.haomeiwen.com/subject/tkhjqctx.html