Ubuntu 18.04 Anaconda使用

Anaconda官网下载

学过Python的估计都知道virtualenv可以用来创建Python的虚拟环境,然而它的一个缺点是难以指定Python的确切版本,比如电脑只安装了Python 3.6,那我们就只能以Python 3.6为基础环境,然后安装任意版本的第三方包。

Anaconda和virtualenv类似,也是用来创建Python的虚拟运行环境,但它的优点就是可以指定任意版本的Python,它把Python也看做是它的一个包,可以方便地安装多个版本。

安装

# wget https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh # 官网
# wget https://mirrors.ustc.edu.cn/anaconda/archive/Anaconda3-5.2.0-Linux-x86_64.sh # 中科大
# bash Anaconda3-5.2.0-Linux-x86_64.sh

安装过程中会询问安装位置以及是否加入环境变量,默认可以安装在~/anaconda3,并且在~/.bashrc文件中增加以下行:

# added by Anaconda3 installer
export PATH="/home/gld/anaconda3/bin:$PATH"

自动化安装

# bash Anaconda3-5.2.0-Linux-x86_64.sh -b -u

-b:批模式,也就是不用输入各种Yes,但是这样不会在~/.bashrc文件中添加环境变量
-u:更新模式,会覆盖旧文件

使用

基本使用

Anaconda的基本使用看这一篇文章足矣:Anaconda使用总结

conda create --name python369 python=3.6.9 # 创建一个Python 3.6.9版本的环境

这里还有一些上面这篇文章没提到,但也常用到的命令:

查看conda的基本信息:

$ conda info

     active environment : None
       user config file : /home/gld/.condarc
 populated config files : /home/gld/.condarc
          conda version : 4.5.11
    conda-build version : 3.10.5
         python version : 3.6.5.final.0
       base environment : /home/gld/anaconda3  (writable)
           channel URLs : https://mirrors.ustc.edu.cn/anaconda/pkgs/main/linux-64
                          https://mirrors.ustc.edu.cn/anaconda/pkgs/main/noarch
                          https://mirrors.ustc.edu.cn/anaconda/pkgs/free/linux-64
                          https://mirrors.ustc.edu.cn/anaconda/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/linux-64
                          https://repo.anaconda.com/pkgs/pro/noarch
          package cache : /home/gld/anaconda3/pkgs
                          /home/gld/.conda/pkgs
       envs directories : /home/gld/anaconda3/envs
                          /home/gld/.conda/envs
               platform : linux-64
             user-agent : conda/4.5.11 requests/2.18.4 CPython/3.6.5 Linux/4.15.0-35-generic ubuntu/18.04 glibc/2.27
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False

查看已有环境:

$ conda info -e

# conda environments:
#
base                  *  /home/gld/anaconda3
python36                 /home/gld/anaconda3/envs/python36

Anaconda官方仓库

所有人都可以在Anaconda Cloud上发布公开包或私有包,在上面可以搜索到包所在频道和安装方法,比如Gurobi:这里写图片描述

# conda install -c gurobi gurobi

表示在gurobi频道安装gurobi包

镜像仓库

官方仓库服务器在国外,有时下载会很慢,因此可以通过国内镜像服务器来加速,我们这里选择中科大镜像源

默认情况下:# conda config --show

...
channel_alias: https://conda.anaconda.org
channel_priority: True
channels:
  - defaults
client_ssl_cert: None
client_ssl_cert_key: None
clobber: False
create_default_packages: []
custom_channels:
  pkgs/main: https://repo.anaconda.com
  pkgs/free: https://repo.anaconda.com
  pkgs/r: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
custom_multichannels:
  defaults: ["https://repo.anaconda.com/pkgs/main", "https://repo.anaconda.com/pkgs/free", "https://repo.anaconda.com/pkgs/r", "https://repo.anaconda.com/pkgs/pro"]
  local: []
default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/free
  - https://repo.anaconda.com/pkgs/r
  - https://repo.anaconda.com/pkgs/pro
...

添加USTC仓库镜像:

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/

conda config --set show_channel_urls yes # 下载包时显示频道URL

之后,# conda config --show

...
channel_alias: https://conda.anaconda.org
channel_priority: True
channels:
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/ # 1. 这行
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/ # 2. 这行
  - defaults
client_ssl_cert: None
client_ssl_cert_key: None
clobber: False
create_default_packages: []
custom_channels:
  pkgs/main: https://repo.anaconda.com
  pkgs/free: https://repo.anaconda.com
  pkgs/r: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
custom_multichannels:
  defaults: ["https://repo.anaconda.com/pkgs/main", "https://repo.anaconda.com/pkgs/free", "https://repo.anaconda.com/pkgs/r", "https://repo.anaconda.com/pkgs/pro"]
  local: []
default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/free
  - https://repo.anaconda.com/pkgs/r
  - https://repo.anaconda.com/pkgs/pro
...

卸载

直接删除目录~/anaconda3以及删除~/.bashrc里相应的内容即可。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页