【问题标题】:How to solve the issue of the conflict of anaconda and virtualenv如何解决anaconda和virtualenv冲突的问题
【发布时间】:2016-07-06 09:54:08
【问题描述】:

我一直在使用 virtualenv,然后我还安装了 anaconda。刚才我尝试使用像source activate helloworld这样的anaconda方式激活一个虚拟环境。 (事实上​​,我不记得这是否正是我输入的命令)。然后环境就被激活了。但是当我尝试运行 notebook 时,据说有些库不存在,即使我已经在那个环境中安装了它们。直到那时我才意识到我激活了错误的环境。然后我关闭选项卡和cdhellowworld 并做了source bin/activate。但为时已晚了。我得到了这个作为输出prepending /home/lcc/anaconda3/envs/bin to PATH 并且环境没有按预期激活。你知道如何解决这个问题吗?谢谢!

这里是/hello/worldactivate文件的完整内容:

#!/bin/bash

# Determine the directory containing this script
if [[ -n $BASH_VERSION ]]; then
    _SCRIPT_LOCATION=${BASH_SOURCE[0]}
    SHELL="bash"
elif [[ -n $ZSH_VERSION ]]; then
    _SCRIPT_LOCATION=${funcstack[1]}
    SHELL="zsh"
else
    echo "Only bash and zsh are supported"
    return 1
fi
_CONDA_DIR=$(dirname "$_SCRIPT_LOCATION")

if [ $# -gt 1 ]; then
    (>&2 echo "Error: did not expect more than one argument.")
    (>&2 echo "    (Got $@)")
    return 1
fi

case "$(uname -s)" in
    CYGWIN*|MINGW32*|MSYS*)
        EXT=".exe"
        ;;
    *)
        EXT=""
        ;;
esac

# Export whatever PS setting we have, so it is available to Python subprocesses
export PS1

# Ensure that this script is sourced, not executed
# Also note that errors are ignored as `activate foo` doesn't generate a bad
# value for $0 which would cause errors.
if [[ -n $BASH_VERSION ]] && [[ "$(basename "$0" 2> /dev/null)" == "activate" ]]; then
    (>&2 echo "Error: activate must be sourced. Run 'source activate envname'
instead of 'activate envname'.
")
    "$_CONDA_DIR/conda" ..activate $SHELL$EXT -h
    exit 1
fi

"$_CONDA_DIR/conda" ..checkenv $SHELL$EXT "$@"
if (( $? != 0 )); then
    return 1
fi

# Ensure we deactivate any scripts from the old env
#   Note: this empties $@.  Preserve a copy.
args=$@
source "$_CONDA_DIR/deactivate"

_NEW_PATH=$("$_CONDA_DIR/conda" ..activate $SHELL$EXT "$args")
if (( $? == 0 )); then
    export CONDA_PATH_BACKUP="$PATH"
    # export this to restore it upon deactivation
    export CONDA_OLD_PS1=$PS1

    export PATH="$_NEW_PATH"
    # Get first path (should be full path prefix of our env)
    # inner string extraction pulls off first path
    # outer string removes /bin if present (on Unix)
    firstpath=${PATH%%:*}
    export CONDA_DEFAULT_ENV="$(echo ${firstpath} | sed "s|/bin$||")" &>/dev/null
    # Legacy support: CONDA_DEFAULT_ENV used to be either env name or full path if given as path.
    #    CONDA_DEFAULT_ENV is now always full path.
    #    Keep CONDA_ENV_PATH around, and have it mirror CONDA_DEFAULT_ENV.
    #    Last date of change: 2016-04-18
    export CONDA_ENV_PATH=$CONDA_DEFAULT_ENV

    export PS1="$( "$_CONDA_DIR/conda" ..setps1 $SHELL$EXT "$args" )"

    # Load any of the scripts found $PREFIX/etc/conda/activate.d AFTER activation
    _CONDA_D="${CONDA_DEFAULT_ENV}/etc/conda/activate.d"
    if [[ -d "$_CONDA_D" ]]; then
        IFS=$(echo -en "\n\b")&>/dev/null  && for f in $(find "$_CONDA_D" -iname "*.sh"); do source "$f"; done
    fi
else
    return $?
fi

if [[ -n $BASH_VERSION ]]; then
    hash -r
elif [[ -n $ZSH_VERSION ]]; then
    rehash
else
    echo "Only bash and zsh are supported"
    return 1
fi

【问题讨论】:

    标签: python ubuntu-14.04 virtualenv anaconda


    【解决方案1】:

    从您的问题中不清楚您期望“激活”做什么。是激活 virtualenv,还是激活 conda 环境。

    安装 miniconda 或 anaconda 时,您可以选择将其添加到 PATH。如果你这样做,那么它可能总是出现在你的 virtualenv 激活脚本之前。您可以重命名一个或另一个,或为 virtualenv 创建一个别名,该别名使用激活脚本的绝对路径调用它。您也可以移动 Anaconda,使其附加而不是前置,但将始终使用 virtualenv activate 而不是 conda(绝对路径除外)。

    要附加 conda 的路径,请查看 ~/.bashrc 或 ~/.bash_profile 并更改

    export PATH=<anaconda path>:$PATH
    

    export PATH=$PATH:<anaconda path>
    

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2020-10-02
      • 2016-04-28
      • 2012-07-27
      相关资源
      最近更新 更多