【Python】Python 环境管理工具 Anaconda 详细介绍(安装 + 具体使用 + 常见问题 及 解决对策)

本文对 Python 虚拟环境工具(环境管理工具) Anaconda 进行了详细的整理,内容包括:

  • Anaconda 诞生的初衷
  • Anaconda 的安装和使用方法
  • 使用 Anaconda 时,可能遇到的问题 和 解决的对策

什么是 Anaconda?

要了解 Anaconda,我们需要先从 Python 本身说起。

Python 环境介绍

编译器(编译环境):

C++ 也好,Python 也好,我们在使用编程语言编写程序之前需要下载对应的 编译器 / 解释器, 也就是 对代码进行编译和执行的东西

Python 目录结构 如下图所示
在这里插入图片描述
其中的 python.exe, 也就是 Python 的解释器

除此之外还有个很重要的东西,Lib,也就是 python 的包(库文件),包括自带的包和第三方包。第三方包我们一般通过 pip 或者 easy_install 来下载。当一个 python 环境中不包含这个包, 那么调用了这个包的程序不能在该python环境中运行。

原生 Python 环境问题分析

使用原生的 Python 环境可能会遇到下面的问题:

1、Python2 Python3 版本切换

我们拿 Python2 和 Python3 来简单举例

我们知道,Python3 对 Python2 是不向下兼容的。虽然 现在 Python3 是主流,入门也往往从 Python3 直接学起。但是也时常会碰见一些用 Python2 写的(老)程序。

常见的一种思路是 python2 和 python3 都安装,两者共存。但这样的话,环境变量 (即默认的 python 版本) 到底是设置成 python2 还是 python3 的目录?如果 需要切换 python 版本,岂不是需要来回修改环境变量?

2、包管理

如果我在本地只有一个 python 环境,那我 所有程序用到的各种包都只能放到同一个环境中, 导致环境很混乱

当我将写好的程序放到另一电脑上运行时,又会遇到缺少相关包,需要自己手动一个个下载的情况,非常麻烦。

我们希望 每个程序开发都能够选用不同的环境, 而开发好之后又能将该程序需要的环境(第三方包)都独立打包出来

Anaconda正是为了解决这些问题而诞生的。


安装 Anaconda

官网下载链接

在这里插入图片描述

① Windows 环境下的安装

Anaconda在windows安装与环境配置

  • 下载 Windows系统,Python3,64位 的安装包,下载完成后运行
  • 安装过程中会遇到如下选项
    在这里插入图片描述
    • 第一个选项是,是否 “将 Anaconda 添加到环境变量”,这涉及到能否 直接在 cmd 中使用 conda、jupyter、ipython 等命令,建议打勾
    • 第二个选项是,是否 “把 Anaconda自带的Python 设置为 系统默认的Python”,也勾上。

② Linux 环境下的安装

下载 Linux 对应的 Python3,x86 的安装包,是一个 .sh 脚本文件。

下载完成后,cd 到 安装包所在目录,执行安装指令:

sudo bash Anaconda3-2020.02-Linux-x86_64.sh

这里要一直按回车,直到出现“是否接受协议”,选择 yes

在这里插入图片描述
设置安装路径,我们更换为 /usr/local/anaconda3【一般ubuntu软件都安装在这个目录下】,因为 /usr/local 文件夹需要root权限进行操作,所以我们在最开始的安装指令前加了 sudo
在这里插入图片描述
询问是否向导入环境变量,这边我们选择no,等后面手动导入。

安装完成后,我们在 /etc/profile 文件末尾添加环境变量,之所以要手动导入,是因为安装包默认是往 ~/.bashrc 导入环境变量,这样服务器上的其他用户是无法直接使用的。

sudo vim /etc/profile
export PATH=/usr/local/anaconda3/bin:$PATH
source /etc/profile

输入 conda info,如果正常跳出以下信息,则说明安装完成

hejian@dell:~$ conda info

     active environment : None
       user config file : /home/hejian/.condarc
 populated config files : /home/hejian/.condarc
          conda version : 4.8.3
    conda-build version : 3.18.11
         python version : 3.8.3.final.0
       virtual packages : __cuda=11.0
                          __glibc=2.23
       base environment : /usr/local/anaconda3  (read only)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /usr/local/anaconda3/pkgs
                          /home/hejian/.conda/pkgs
       envs directories : /home/hejian/.conda/envs
                          /usr/local/anaconda3/envs
               platform : linux-64
             user-agent : conda/4.8.3 requests/2.24.0 CPython/3.8.3 Linux/4.15.0-112-generic ubuntu/16.04.3 glibc/2.23
                UID:GID : 1003:1003
             netrc file : None
           offline mode : False

Anaconda 用法

基本操作

① 进入环境

我们通过 conda activate 指令来进入到 anaconda 设定的虚拟环境中。如果不带参数则会进入 anaconda 自带的 base 环境,

hejian@dell:/$ conda activate
(base) hejian@dell:/$ 

进入 base 环境后,在命令行前面会多一个 (base),说明当前我们处于的是base环境下。

此时输入 python, 则会进入 base 环境的 python 解释器,而不是你原来的 python 解释器

(base) hejian@dell:/$ python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

如果需要退出环境,则使用 conda deactivate 指令

(base) hejian@dell:/$ conda deactivate
hejian@dell:/$ 

② 创建环境

如果我们要创建一个新的虚拟环境,可以输入以下指令

conda create --name your_env_name  

或者

conda create -n your_env_name

其中,your_env_name 就是你新创建的环境名

你可以在创建环境时 指定 python版本 和 包内容

conda create -n your_env_name python=3.5 numpy pandas

如果要 指定包的版本,在包名后面加上 =版本号 就行,默认是最新的【如果指定 python=3,则 conda 会自动找到 python3 中最新的版本下载】

③ 显示所有的环境

我们可以使用 conda env list 指令 来 查看当前的所有环境

hejian@dell:/$ conda env list
# conda environments:
#
base                  *  /opt/app/anaconda3

④ 删除环境

conda remove -n xxxx --all

⑤ 包管理

除了使用 python 自己的 pip 来安装更新删除包以外,我们还可以用 conda 来管理包

常用的指令如下

conda list  # 列举当前环境下的所有包
conda list -n packagename  # 列举某个特定名称包
conda install packagename  # 为当前环境安装某包
conda install -n envname packagename  # 为某环境安装某包
conda search packagename  # 搜索某包
conda updata packagename  # 更新当前环境某包
conda update -n envname packagename  # 更新某特定环境某包
conda remove packagename  # 删除当前环境某包
conda remove -n envname packagename  # 删除某环境环境某包

conda 本身和 anaconda、python 本身也算包

conda update conda
conda update anaconda
conda update python

⑥ 换源

conda 默认源的下载速度比较慢,一般我们从国内源下载。常用的国内源有

比较常用的换源方式有两种(下面以清华源举例):

方式一、shell命令

# 添加anaconda仓库
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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

conda config --set show_channel_urls yes

其中,最后一条命令能够在安装包的时候显示包的来源

在这里插入图片描述

教育网用户则可以添加ipv6源

conda config --add channels https://mirrors6.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors6.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

conda config --set show_channel_urls yes

添加源后可以用 conda info 来查看当前源的信息

hejian@dell:/$ conda info

     active environment : None
            shell level : 0
       user config file : /home/hejian/.condarc
 populated config files : /home/hejian/.condarc
          conda version : 4.8.4
    conda-build version : 3.18.11
         python version : 3.7.6.final.0
       virtual packages : __cuda=9.0
                          __glibc=2.23
       base environment : /opt/app/anaconda3  (writable)
           channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
                          https://mirrors.tuna.tsinghua.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/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/app/anaconda3/pkgs
                          /home/hejian/.conda/pkgs
       envs directories : /opt/app/anaconda3/envs
                          /home/hejian/.conda/envs
               platform : linux-64
             user-agent : conda/4.8.4 requests/2.22.0 CPython/3.7.6 Linux/4.15.0-101-generic ubuntu/16.04.3 glibc/2.23
                UID:GID : 1003:1003
             netrc file : None
           offline mode : False

如果需要删除源,则需要使用以下命令

conda config -remove-key channels https://repo.continuum.io/pkgs/main/

方式二、修改 .condarc 配置文件

Linux 中,该文件位于用户目录下(~/.condarc)

将以下内容复制粘贴到文件中即可:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

其他一些操作

环境的导入导出

[1] pip和conda批量导出、安装组件(requirements.txt)

[2] conda 迁移环境

conda自动激活的开启和关闭

安装 aconda 后Linux的终端界面前部出现(base)字样

conda config --set auto_activate_base false  # 关闭自动激活状态
conda config --set auto_activate_base true  # 关闭自动激活状态

Anaconda 常见问题整理

1、电脑已经安装了 python,可以直接安装 Anaconda 吗?

两者不冲突。

电脑原本的 python 环境和 Anaconda 环境,你可以看成是相互独立的,关键看你环境变量设置成谁。

如何在已安装Python条件下,安装Anaconda,,并将原有Python添加到Anaconda中

2、Anaconda2,Anaconda3有啥区别?

Anaconda 有 python2 和 python3 版本,对应 Anaconda2 和 Anaconda3。

个人理解,这所谓的 python2 和 python3 指的是 Anaconda 自带的 base 环境的 Python 的版本。打个比方,如果我安装了 Anaconda3,其实指的是配套的base环境的python版本是3.7.6,除此之外 Anaconda2 和 Anaconda3 是完全一样的,你可以用 conda 创建任意 python2,python3 版本的环境。

3、Found conflicts! Looking for incompatible packages.

报错信息打印

(base) hejian@dell:/$ conda remove pytorch
Collecting package metadata (repodata.json): done
Solving environment: \ 
Found conflicts! Looking for incompatible packages.
Examining conflict for anaconda openssl python certifi conda-verify conda _ipyw_jlab_nb_ext_conf anaconda-navigator navigator-updater ca-certificates conda-build: : 18it [02:24,  5.54s/itExamining conflict for anaconda openssl python certifi conda-verify conda _ipyw_jlab_nb_ext_conf anaconda-navigator navigator-updater ca-certificates conda-build: : 19it [02:24,  5.39s/i\Examining conflict for anaconda-navigator anaconda conda-build: : 19it [02:25,  5.39s/it]                                                                                                failed                                                                                                      

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - conda-build -> python[version='>=3.4,<3.5.0a0']
  - conda-verify -> python[version='>=3.8,<3.9.0a0']
  - conda[version='>=4.8.4'] -> python[version='>=3.5,<3.6.0a0']

Your python: python=3.7

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with each other:

Output in format: Requested package -> Available versions

Package jinja2 conflicts for:
_ipyw_jlab_nb_ext_conf -> jupyterlab -> jinja2[version='>=2.10']
conda-build -> jinja2
anaconda==2020.02=py37_0 -> bokeh==1.4.0=py37_0 -> jinja2[version='>2.10*|>=2.10|>=2.7|>=2.3']
anaconda==2020.02=py37_0 -> jinja2==2.11.1=py_0
conda-verify -> jinja2

Package future conflicts for:
conda-verify -> future
conda-build -> conda-verify -> future
anaconda==2020.02=py37_0 -> python-jsonrpc-server==0.3.4=py_0 -> future
anaconda==2020.02=py37_0 -> future==0.18.2=py37_0
...

原因

不同包的版本有冲突,可能是因为之前安装包的姿势不对

解决方案

最简单粗暴有效的方法,创建新的环境。
【所以,有啥配置需求,新建一个环境就行,随你折腾,出问题大不了删了重搞】

参考资料

python - Conda安装和更新不起作用也解决了环境获取错误

4、NotWritableError: The current user does not have write permissions to a required path.

原因

当前用户没有对anaconda3文件夹的读写权限【可能是安装 Anaconda 的时候使用了 root 权限】。

解决办法

更改文件夹权限为任何用户可读可写

sudo chmod -R 777 anaconda3

其中,777表示文件最高权限(任何用户都可读可改可写),-R 表示此目录下的子文件也同时更改

参考资料

5、CondaError: Downloaded bytes did not match Content-Length

报错信息打印

CondaError: Downloaded bytes did not match Content-Length
  url: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudatoolkit-10.2.89-hfd86e86_1.conda
  target_path: /usr/local/anaconda3/pkgs/cudatoolkit-10.2.89-hfd86e86_1.conda
  Content-Length: 382791771
  downloaded bytes: 310247084

原因

在用conda安装包的时候下载包的长度不够导致安装包不成功。原因一般是 下载的速度较慢,下载因为 timeout(超时) 而终止。

解决方法

对策一、换源,从速度快的源下载

对策二、 设置 conda 下载的 timeout 上限,如果时长不够可以设置的大一点

conda config --set remote_read_timeout_secs 600.0  # 600s,也就是 10min

对策三、直接从 conda 源下载好要安装的包,采用 本地安装 的方式

包的下载地址和存放目录在报错信息中已经给出了:

  url: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudatoolkit-10.2.89-hfd86e86_1.conda
  target_path: /usr/local/anaconda3/pkgs/cudatoolkit-10.2.89-hfd86e86_1.conda

下载好之后使用以下指令进行安装:

conda install --offline /usr/local/anaconda3/pkgs/cudatoolkit-10.2.89-hfd86e86_1.conda

参考资料

Conda - Downloaded bytes did not match Content-Length 问题解决方案

6、Executing transaction: | WARNING conda.core.envs_manager:register_env(50): Unable to register environment. Path not writable or missing.

报错信息打印

Executing transaction: | WARNING conda.core.envs_manager:register_env(50): Unable to register environment. Path not writable or missing.
  environment location: /usr/local/anaconda3/envs/pytorch160
  registry file: /home/hejian/.conda/environments.txt                            done

原因

和问题3类似,同样是对 /home/hejian/.conda/environments.txt 文件没有修改权限

解决方法

更改文件权限

sudo chown -R hejian /home/hejian/.conda

参考资料

Ubuntu18.04 安装 Conda 遇到的问题和集(一)2020.5.29

7、CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate’.

报错信息打印

hejian@dell:~$ conda activate pytorch1.6.0

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

原因

之前激活环境后未使用 conda deactivate 退出环境就关闭了终端。

解决方法

用 source 命令激活环境即可

source activate pytorch1.6.0

参考资料

conda activate激活环境出错的解决办法


参考资料

[1] Anaconda完全入门指南

[2] Anaconda简单入门

[3] conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值