You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Anaconda同时使用Python3.8与2.7?或修改base环境python链接?

解决Ubuntu 18.04下同时使用Python 3.8和2.7编译Chrome V8的问题

一、修复update-alternatives导致的系统异常

之前修改Python默认替代项引发系统异常,可按以下步骤恢复Ubuntu 18.04默认配置:

  1. 查看当前Python替代配置:
    update-alternatives --list python
    update-alternatives --list python3
    
  2. 移除错误添加的替代项(比如之前加入的Python3.8):
    update-alternatives --remove python3 /usr/bin/python3.8
    # 其他错误项同理移除
    
  3. 重新设置系统默认Python替代:
    # 设置Python2默认指向2.7
    update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
    # 设置Python3默认指向3.6
    update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
    
  4. 验证修复结果:
    python --version  # 应显示Python 2.7.x
    python3 --version # 应显示Python 3.6.x
    

二、正确配置Anaconda实现Python版本切换

你遇到的base环境调用系统Python3.6问题,大概率是Anaconda路径未优先于系统PATH导致,可按以下操作修正:

  1. 确保Anaconda路径在系统PATH最前端:
    打开~/.bashrc文件,添加或确认以下内容(替换为你的Anaconda实际安装路径):
    # Anaconda初始化
    __conda_setup="$('/home/你的用户名/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
        eval "$__conda_setup"
    else
        if [ -f "/home/你的用户名/anaconda3/etc/profile.d/conda.sh" ]; then
            . "/home/你的用户名/anaconda3/etc/profile.d/conda.sh"
        else
            export PATH="/home/你的用户名/anaconda3/bin:$PATH"
        fi
    fi
    unset __conda_setup
    
    执行生效命令:
    source ~/.bashrc
    
  2. 确认base环境为Python3.8:
    conda activate base
    python --version
    
    若版本不对,重新安装base环境的Python3.8:
    conda install python=3.8 -n base
    
  3. 通过虚拟环境切换到Python2.7:
    激活你已创建的v8环境即可使用Python2.7:
    conda activate v8
    python --version  # 应显示Python 2.7.x
    
    退出环境自动切回base的Python3.8:
    conda deactivate
    

三、关于将Anaconda base环境python链接到系统python2.7的问题

绝对不建议这么操作。Anaconda的base环境依赖自身的Python解释器,硬修改python链接会破坏conda核心依赖,导致conda命令失效、整个环境崩溃。正确做法是通过虚拟环境切换版本,或在需要时直接调用python2.7(系统预装版本)、激活base环境使用Python3.8。

内容的提问来源于stack exchange,提问作者Gunseung Jin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:27:13