Anaconda

Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存、切换以及各种第三方包安装问题。

它可以用来管理python版本,当然,也可以用virtualenv工具管理虚拟环境。我个人更喜欢Anaconda,因为他比虚拟环境方便。

下载

Anaconda官网下载,有400多M,直接从官网下非常慢

清华大学开源软件镜像站或者中科大开源镜像站提供了Anaconda镜像

下载的时候下对需要的版本,Anaconda2对应的是python2,Anaconda3对应的是python3,Anaconda的版本并不重要 ,因为我们可以通过环境管理来切换python版本。

安装

我用的是opensuse系统,下载Anaconda3-4.4.0-Linux-x86_64.sh,加下权限执行即可,然后根据提示按回车、设置安装路径、加PATH

测试:终端输入conda env list

# conda environments:
#
root                  *  /home/tutu/anaconda3

环境管理

配置下载地址

因为使用默认的地址下载速度太慢了,因此需要设置国内的镜像站(这个和设置源差不多意思)

# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# TUNA的help中镜像地址加有引号,需要去掉

# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
# 完成之后在~/.condarc里面就会写入配置

python环境管理

# 创建一个名为python34的环境,指定Python版本是3.4(不用管是3.4.x,conda会为我们自动寻找3.4.x中的最新版本)
conda create --name python34 python=3.4

# 安装好后,使用activate激活某个环境
activate python34 # for Windows
source activate python34 # for Linux & Mac
# 激活后,会发现terminal输入的地方多了python34的字样,实际上,此时系统做的事情就是把默认2.7环境从PATH中去除,再把3.4对应的命令加入PATH

# 此时,再次输入
python --version
# 可以得到`Python 3.4.5 :: Anaconda 4.1.1 (64-bit)`,即系统已经切换到了3.4的环境

# 如果想返回默认的python 2.7环境,运行
deactivate python34 # for Windows
source deactivate python34 # for Linux & Mac

# 删除一个已有的环境
conda remove --name python34 --all

安装的python都会放在anaconda3/envs目录下,使用conda env list查看当前己经安装了哪些环境,激活的环境以星号标记。

如果之前就己经安装了python,Anaconda是后面装的,可以将整个python目录剪切到anaconda3/envs下,通过Anaconda来管理。

包管理

# 安装scrapy
conda install scrapy
# conda会从从远程搜索scrapy的相关信息和依赖项目

# 查看已经安装的packages
conda list
# 最新版的conda是从site-packages文件夹中搜索已经安装的包,不依赖于pip,因此可以显示出通过各种方式安装的包

常用操作

# 查看当前环境下已安装的包
conda list

# 查看某个指定环境的已安装包
conda list -n python27

# 查找包信息
conda search requests

# 安装包
conda install -n python27 requests
# 如果不用-n指定环境名称,则被安装在当前活跃环境
# 也可以通过-c指定通过某个channel(镜像)安装

# 更新包
conda update -n python27 requests

# 删除包
conda remove -n python27 requests

有时候在当前channel找不到需要装的包

tutu@linux-xk1e:~> conda install xmltodict
Fetching package metadata ...........

PackageNotFoundError: Packages missing in current channels:

  - xmltodict

We have searched for the packages in the following channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - https://repo.continuum.io/pkgs/free/linux-64
  - https://repo.continuum.io/pkgs/free/noarch
  - https://repo.continuum.io/pkgs/r/linux-64
  - https://repo.continuum.io/pkgs/r/noarch
  - https://repo.continuum.io/pkgs/pro/linux-64
  - https://repo.continuum.io/pkgs/pro/noarch

这时使用anaconda search -t conda xmltodict在anaconda.org中来查找

tutu@linux-xk1e:~>  anaconda search -t conda xmltodict
Using Anaconda API: https://api.anaconda.org
Run 'anaconda show ' to get more details:
Packages:
     Name                      |  Version | Package Types   | Platforms      
     ------------------------- |   ------ | --------------- | ---------------
     PinguCarsti/xmltodict     |    0.8.3 | conda           | linux-64, osx-64
     abgreenwell/xmltodict     |    0.8.3 | conda           | win-64         
     asmeurer/xmltodict        |    0.8.3 | conda           | osx-64         
                                          : https://github.com/martinblech/xmltodict
     auto/xmltodict            |    0.8.6 | conda           | linux-64, linux-32, osx-64
                                          : https://github.com/martinblech/xmltodict
     bioconda/xmltodict        |    0.9.2 | conda           | linux-64, osx-64
                                          : Makes working with XML feel like you are working with JSON
     conda-forge/xmltodict     |   0.11.0 | conda           | linux-64, win-32, osx-64, win-64
                                          : Makes working with XML feel like you are working with JSON
     creditx/xmltodict         |   0.10.2 | conda           | linux-64       
     dan_blanchard/xmltodict   |    0.8.3 | conda           | linux-64       
                                          : https://github.com/martinblech/xmltodict
     davidbgonzalez/xmltodict  |    0.9.2 | conda           | linux-64, osx-64
                                          : Makes working with XML feel like you are working with JSON
     derickl/xmltodict         |   0.10.2 | conda           | osx-64         
     dimazest/xmltodict        |    0.9.2 | conda           | linux-64, osx-64
     equipoise/xmltodict       |    0.9.2 | conda           | osx-64, win-64
     farrajota/xmltodict       |   0.11.0 | conda           | linux-64, win-32, win-64, linux-32, osx-64
                                          : Makes working with XML feel like you are working with JSON
     hargup/xmltodict          |          | conda           | linux-64, noarch
                                          : Makes working with XML feel like you are working with JSON
     indico/xmltodict          |    0.9.2 | conda           | linux-64       
                                          : Makes working with XML feel like you are working with JSON
     jacksongs/xmltodict       |   0.10.2 | conda           | osx-64         
     jgreeley/xmltodict        |   0.10.2 | conda           | linux-64       
     kabaka0/xmltodict         |    0.9.2 | conda           | linux-64       
                                          : Makes working with XML feel like you are working with JSON
     luzazul45/xmltodict       |    0.9.2 | conda           | win-64         
                                          : Makes working with XML feel like you are working with JSON
     mathieu/xmltodict         |   0.10.1 | conda           | osx-64         
                                          : Makes working with XML feel like you are working with JSON
     mh00h/xmltodict           |    0.9.2 | conda           | linux-64       
                                          : Makes working with XML feel like you are working with JSON
     moustik/xmltodict         |    0.9.2 | conda           | linux-64       
     rsmulktis/xmltodict       |   0.10.2 | conda, pypi     | linux-64, win-64
     russellthomas/xmltodict   |    0.9.0 | conda           | osx-64         
                                          : xmltodict is a simple library that aims at making XML feel like working with JSON
     xavier/xmltodict          |    0.5.1 | conda           | linux-64       
                                          : https://github.com/martinblech/xmltodict
Found 25 packages

选择一个合适的版本进行安装,如:

conda install -c https://conda.anaconda.org/conda-forge xmltodict
或者
# cconda install -c "name" "packagename"
conda install -c conda-forge xmltodict

当然,也可以通过pip安装

pip install xmltodict

更新包

# 更新conda,保持conda最新
conda update conda

# 更新anaconda
conda update anaconda

# 更新python
conda update python
# 假设当前环境是python 3.4, conda会将python升级为3.4.x系列的当前最新版本

你可能感兴趣的:(Anaconda)