Skip to main content
 首页 » 编程设计

python之Bash 使用来自 Anaconda 的 Python,即使它已被停用

2024年11月01日8lexus

总结:即使我使用 conda deactivate 停用 conda 环境,当我键入 python(或 python3)时在 bash 上,来自 conda 的 python 启动。

背景: 通常我使用 pipenv 来处理 python 依赖项。但早些时候我也安装了 conda,用 GPU 训练一些 ML 模型,因为我无法使用 pipenv 管理运行 GPU。

但我真的不想同时使用 conda 和 pipenv,所以我决定摆脱 conda,看看我能否仅使用 pipenv 来运行 GPU。

但是当我尝试停用 conda 并使用默认 python 时,它仍然存在:

(base) user@tower:~$ conda deactivate 
user@tower:~$ python 
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)  
[GCC 7.3.0] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
user@tower:~$ python3 
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)  
[GCC 7.3.0] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>>  

你有什么建议,我可以尝试在不使用 conda 的情况下运行一些 python 代码吗? 如果我不能,我如何才能完全摆脱 conda?

我愿意接受任何建议。

信息 1: $PATH 停用前后的输出是一样的:

-bash: /home/foouser/.local/bin:/home/foouser/.local/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/condabin:/usr/local/cuda-9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/foouser/.vimpkg/bin: No such file or directory 

~/.bashrc 中的 Anaconda 部分:

# added by Anaconda3 2018.12 installer 
# >>> conda init >>> 
# !! Contents within this block are managed by 'conda init' !! 
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/foouser/anaconda3/bin/conda' shell.bash hook 2> /dev/null)" 
if [ $? -eq 0 ]; then 
    \eval "$__conda_setup" 
else 
    if [ -f "/home/foouser/anaconda3/etc/profile.d/conda.sh" ]; then 
        . "/home/foouser/anaconda3/etc/profile.d/conda.sh" 
        CONDA_CHANGEPS1=false conda activate base 
    else 
        \export PATH="/home/foouser/anaconda3/bin:$PATH" 
    fi 
fi 
unset __conda_setup 
# <<< conda init <<< 
 
# added by Anaconda3 installer 
export PATH="/home/foouser/anaconda3/bin:$PATH" 
 
export PATH="/home/foouser/.local/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/condabin:/usr/local/cuda-9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/foouser/.vimpkg/bin" 

信息 2: which -a python 的输出:

/home/foouser/anaconda3/bin/python 
/usr/bin/python 

Edit-1: 根据建议编辑 bashrc:

export PATH=/usr/local/cuda-9.0/bin:$PATH 
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH 
 
export PATH="/home/foouser/.local/bin:$PATH" 
 
# added by Anaconda3 2018.12 installer 
# >>> conda init >>> 
# !! Contents within this block are managed by 'conda init' !! 
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/foouser/anaconda3/bin/conda' shell.bash hook 2> /dev/null)" 
if [ $? -eq 0 ]; then 
    \eval "$__conda_setup" 
else 
    if [ -f "/home/foouser/anaconda3/etc/profile.d/conda.sh" ]; then 
        . "/home/foouser/anaconda3/etc/profile.d/conda.sh" 
        CONDA_CHANGEPS1=false conda activate base 
    else 
        \export PATH="/home/foouser/anaconda3/bin:$PATH" 
    fi 
fi 
unset __conda_setup 
# <<< conda init <<< 
 
# added by Anaconda3 installer 
# export PATH="/home/foouser/anaconda3/bin:$PATH" 
 
# export PATH="$PATH:/home/foouser/.vimpkg/bin" 
# export PATH="/home/foouser/.local/bin:$PATH" 
# export PATH="/home/foouser/.local/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/condabin:/usr/local/cuda-9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/foouser/.vimpkg/bin"                                                                                                                                                                                         

请您参考如下方法:

问题来自以下几行,这与 the post-Conda v.4.4 best practices 相违背(即,不要手动操作 PATH 来添加 Conda):

# added by Anaconda3 installer 
export PATH="/home/foouser/anaconda3/bin:$PATH" 
 
export PATH="/home/foouser/.local/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/bin:/home/foouser/anaconda3/condabin:/usr/local/cuda-9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/foouser/.vimpkg/bin" 

第一个应该完全删除。第二个应该删除所有包含 anaconda3 的路径。此外,它会完全覆盖您原来的 PATH。您应该对其进行返工,以便只有您需要手动添加的内容才会被添加到前面,例如

export PATH="/home/foouser/.local/bin:$PATH" 

或附加,例如,

export PATH="$PATH:/home/foouser/.vimpkg/bin" 

我还将其移至 # >>> conda init >>> 部分之前。