如何解决anaconda与virtualenv冲突的问题 [英] How to solve the issue of the conflict of anaconda and virtualenv

查看:504
本文介绍了如何解决anaconda与virtualenv冲突的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都在使用virtualenv,然后还安装了anaconda.刚才我尝试使用source activate helloworld之类的anaconda方式激活虚拟环境. (实际上,我不记得这是否正是我键入的命令).然后环境被激活了.但是,当我尝试运行笔记本时,据说即使我已经在该环境中安装了某些库,它们也不存在.直到那时,我才意识到自己激活了错误的环境.然后关闭选项卡,将cd更改为hellowworld,然后执行source bin/activate.但为时已晚.我将其作为输出prepending /home/lcc/anaconda3/envs/bin to PATH 且预期未激活环境.你知道如何解决这个问题吗?谢谢!

I was using virtualenv all the time and then I had also installed anaconda. Just now I tried to activate a virtual environment using the way of anaconda like source activate helloworld. (Indeed I don't remember if this is exactly the command I typed in). And then the environment had been activated. But when I tried to run the notebook, it was said that some libraries didn't exist even if I had installed them already in that environment. Not until then did I realize I had activated the wrong environment. And then I close the tab and cd to hellowworld and did source bin/activate. But it was too late. I got this as the output prepending /home/lcc/anaconda3/envs/bin to PATH and the environment was not activated expectedly. Do you know how to solve this issue? Thanks!

这是/hello/worldactivate文件的全部内容:

Here is the full content of the activate file under /hello/world:

#!/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

推荐答案

从您的问题中尚不清楚您期望激活"做什么.它是在激活virtualenv还是在激活conda环境.

It's not clear from your question what you expect "activate" to do. Is it activating a virtualenv, or is it activating a conda environment.

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

When you install miniconda or anaconda, you are given an option to add it to PATH. If you do so, then it probably always comes before your virtualenv activate script. You can rename one or the other, or create an alias to the virtualenv one that calls it with an absolute path to the activate script. You can also move Anaconda so that it's appended rather than prepended, but then the virtualenv activate will always be used rather than the conda one (barring absolute paths).

要追加conda的路径,请查看〜/.bashrc或〜/.bash_profile并进行更改

To append conda's path, look in ~/.bashrc or ~/.bash_profile and change

export PATH=<anaconda path>:$PATH

export PATH=$PATH:<anaconda path>

这篇关于如何解决anaconda与virtualenv冲突的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆