• Linux 下安装 miniconda,管理 Python 多环境


    安装 miniconda

    1、下载安装包 Miniconda3-py37_22.11.1-1-Linux-x86_64.sh,或者自行选择版本

    2、把安装包上传到服务器上,这里放在 /home/software

    3、安装

    bash Miniconda3-py37_22.11.1-1-Linux-x86_64.sh 
    
    • 1

    4、按回车

    Welcome to Miniconda3 py37_22.11.1-1
    
    In order to continue the installation process, please review the license
    agreement.
    Please, press ENTER to continue
    >>> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5、按空格跳到最下面,输入yes

    Do you accept the license terms? [yes|no]
    [no] >>>  yes
    
    • 1
    • 2

    6、选择安装位置,这里选择默认,直接回车,有需要可以自己输入改掉

    Miniconda3 will now be installed into this location:
    /root/miniconda3
    
      - Press ENTER to confirm the location
      - Press CTRL-C to abort the installation
      - Or specify a different location below
    
    [/root/miniconda3] >>> 
    PREFIX=/root/miniconda3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    7、初始化 miniconda,输入 yes

    installation finished.
    Do you wish the installer to initialize Miniconda3
    by running conda init? [yes|no]
    [no] >>> yes
    
    • 1
    • 2
    • 3
    • 4

    8、现在 conda 命令是找不到的,需要激活

    source ~/.bashrc
    
    • 1

    激活后可以看到启动了 base 环境,conda 命令也可以用了,下一节会介绍常用 conda 命令。

    9、可以设置启动时,不自动激活 base 环境

    conda config --set auto_activate_base false
    
    • 1

    10、设置 conda 镜像源

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    
    • 1
    • 2

    11、设置 pip 镜像

    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    pip config set install.trusted-host mirrors.aliyun.com
    
    • 1
    • 2

    12、查看 python 环境

    (base) [root@xxx software]# python
    Python 3.7.15 (default, Nov 24 2022, 21:12:53) 
    [GCC 11.2.0] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print('hello')
    hello
    >>> exit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    可以看到默认的 python 环境是 3.7,也就是我们安装的 miniconda 的 python 版本。

    并且 python2 还是存在的。

    (base) [root@xxx software]# python2
    Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print('hello')
    hello
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用 miniconda 管理 python 多环境

    这里简单介绍一些常用的 conda 命令。

    1、查看所有环境

    conda env list
    
    • 1

    2、激活某个环境, 为环境名

    conda activate <name>
    
    • 1

    3、退出当前环境

    conda deactivate
    
    • 1

    4、创建虚拟环境,指定名字和 python 版本

    conda create --name <name> python=3.x
    
    • 1

    5、克隆一个环境

    conda create --name <new_name> --clone <old_name>
    
    • 1

    6、删除某个环境

    conda remove --name <name> --all
    
    • 1

    7、conda 清空缓存

    conda clean -y -all
    
    • 1

    8、conda 镜像源

    # 查看镜像源
    conda config --show-sources
    # 添加镜像源
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    # 从镜像源中安装包时显示来源
    conda config --set show_channel_urls yes
    # 删除镜像源
    conda config --remove channels https://XXX
    # 删除配置的镜像源,使用默认镜像源
    conda config --remove-key channels
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    9、pip 镜像源

    # 查看配置
    pip config list
    # 添加镜像源
    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    pip config set install.trusted-host mirrors.aliyun.com
    # 清除缓存
    rm -rf ~/.cache/pip
    # windows 下 pip 配置文件位置
    C:\Users\Administrator\AppData\Roaming\pip
    # Linux 下 pip 配置文件位置
    /root/.config/pip/pip.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    pip 配置文件参考内容

    [global]
    cache-dir = D:\Environment\Miniconda3\pip\cache
    index-url = https://mirrors.aliyun.com/pypi/simple/
    extra-index-url=
            https://pypi.tuna.tsinghua.edu.cn/simple/
            http://pypi.douban.com/simple/
            http://pypi.mirrors.ustc.edu.cn/simple/
    
    [install]
    trusted-host=
            pypi.tuna.tsinghua.edu.cn
            mirrors.aliyun.com
            pypi.douban.com
            pypi.mirrors.ustc.edu.cn
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    10、导出与导入当前环境配置

    conda env export > environment.yaml
    conda env create -f environment.yaml
    
    • 1
    • 2

    卸载 miniconda

    1、删除 miniconda3 文件夹

    rm -rf ~/miniconda3/
    
    • 1

    2、删除 .conda 文件夹和 condarc 文件

    rm -rf ~/.conda
    rm -rf ~/.condarc
    
    • 1
    • 2

    3、删除配置中 conda 相关

    vim ~/.bashrc
    
    • 1

    删除或注释下面这段

    # >>> conda initialize >>>
    # !! Contents within this block are managed by 'conda init' !!
    __conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
        eval "$__conda_setup"
    else
        if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
            . "/root/miniconda3/etc/profile.d/conda.sh"
        else
            export PATH="/root/miniconda3/bin:$PATH"
        fi
    fi
    unset __conda_setup
    # <<< conda initialize <<<
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4、刷新环境

    source ~/.bashrc
    
    • 1

  • 相关阅读:
    有趣的前端面试题
    IT企业管理
    【六袆 - 前端】JavaScript学习;JavaScript面试题;坐地铁,坐公交,打DD的时候刷一刷
    Ubuntu软件包升级失败的终极修复方法
    JavaScript判断是否为空对象的几种方法
    Git使用教程
    高级IO-epoll
    电子文档管理对企业至关重要的4个原因
    永磁材料测试仪系统全自动测量软件
    [附源码]Python计算机毕业设计Django校园快递柜存取件系统
  • 原文地址:https://blog.csdn.net/qq_44290077/article/details/133911406