200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【Python】Python 环境管理工具 Anaconda 详细介绍(安装 + 具体使用 + 常见问题 及 解决对策)

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

时间:2023-01-16 01:14:58

相关推荐

【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-.02-Linux-x86_64.sh

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

设置安装路径,我们更换为 /usr/local/anaconda3【一般ubuntu软件都安装在这个目录下】,因为 /usr/local 文件夹需要root权限进行操作,所以我们在最开始的安装指令前加了 sudo

询问是否向导入环境变量,这边我们选择no,等后面手动导入。

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

sudo vim /etc/profileexport PATH=/usr/local/anaconda3/bin:$PATHsource /etc/profile

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

hejian@dell:~$ conda infoactive environment : Noneuser config file : /home/hejian/.condarcpopulated config files : /home/hejian/.condarcconda version : 4.8.3conda-build version : 3.18.11python version : 3.8.3.final.0virtual packages : __cuda=11.0__glibc=2.23base environment : /usr/local/anaconda3 (read only)channel URLs : /pkgs/main/linux-64/pkgs/main/noarch/pkgs/r/linux-64/pkgs/r/noarchpackage cache : /usr/local/anaconda3/pkgs/home/hejian/.conda/pkgsenvs directories : /home/hejian/.conda/envs/usr/local/anaconda3/envsplatform : linux-64user-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.23UID:GID : 1003:1003netrc file : Noneoffline 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:/$ pythonPython 3.7.6 (default, Jan 8 , 19:59:22) [GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license" for more information.>>>

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

(base) hejian@dell:/$ conda deactivatehejian@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 condaconda update anacondaconda update python

⑥ 换源

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

清华源中科大源

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

方式一、shell命令

# 添加anaconda仓库conda config --add channels https://mirrors.tuna./anaconda/pkgs/free/conda config --add channels https://mirrors.tuna./anaconda/pkgs/main/# 添加第三方源conda config --add channels https://mirrors.tuna./anaconda/cloud/conda-forge/conda config --add channels https://mirrors.tuna./anaconda/cloud/msys2/conda config --add channels https://mirrors.tuna./anaconda/cloud/bioconda/conda config --add channels https://mirrors.tuna./anaconda/cloud/menpo/conda config --add channels https://mirrors.tuna./anaconda/cloud/pytorch/conda config --set show_channel_urls yes

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

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

conda config --add channels https://mirrors6.tuna./anaconda/pkgs/free/conda config --add channels https://mirrors6.tuna./anaconda/pkgs/main/conda config --set show_channel_urls yes

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

hejian@dell:/$ conda infoactive environment : Noneshell level : 0user config file : /home/hejian/.condarcpopulated config files : /home/hejian/.condarcconda version : 4.8.4conda-build version : 3.18.11python version : 3.7.6.final.0virtual packages : __cuda=9.0__glibc=2.23base environment : /opt/app/anaconda3 (writable)channel URLs : https://mirrors.tuna./anaconda/pkgs/main/linux-64https://mirrors.tuna./anaconda/pkgs/main/noarchhttps://mirrors.tuna./anaconda/pkgs/free/linux-64https://mirrors.tuna./anaconda/pkgs/free/noarch/pkgs/main/linux-64/pkgs/main/noarch/pkgs/r/linux-64/pkgs/r/noarchpackage cache : /opt/app/anaconda3/pkgs/home/hejian/.conda/pkgsenvs directories : /opt/app/anaconda3/envs/home/hejian/.conda/envsplatform : linux-64user-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.23UID:GID : 1003:1003netrc file : Noneoffline mode : False

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

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

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

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

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

channels:- defaultsshow_channel_urls: truedefault_channels:- https://mirrors.tuna./anaconda/pkgs/main- https://mirrors.tuna./anaconda/pkgs/r- https://mirrors.tuna./anaconda/pkgs/msys2custom_channels:conda-forge: https://mirrors.tuna./anaconda/cloudmsys2: https://mirrors.tuna./anaconda/cloudbioconda: https://mirrors.tuna./anaconda/cloudmenpo: https://mirrors.tuna./anaconda/cloudpytorch: https://mirrors.tuna./anaconda/cloudsimpleitk: https://mirrors.tuna./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 pytorchCollecting package metadata (repodata.json): doneSolving 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 foundto 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.7If 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 somehownot available for the python version you are constrained to. Note that conda will notchange your python version to a different minor version unless you explicitly specifythat.The following specifications were found to be incompatible with each other:Output in format: Requested package -> Available versionsPackage jinja2 conflicts for:_ipyw_jlab_nb_ext_conf -> jupyterlab -> jinja2[version='>=2.10']conda-build -> jinja2anaconda==.02=py37_0 -> bokeh==1.4.0=py37_0 -> jinja2[version='>2.10*|>=2.10|>=2.7|>=2.3']anaconda==.02=py37_0 -> jinja2==2.11.1=py_0conda-verify -> jinja2Package future conflicts for:conda-verify -> futureconda-build -> conda-verify -> futureanaconda==.02=py37_0 -> python-jsonrpc-server==0.3.4=py_0 -> futureanaconda==.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-Lengthurl: https://mirrors.tuna./anaconda/pkgs/main/linux-64/cudatoolkit-10.2.89-hfd86e86_1.condatarget_path: /usr/local/anaconda3/pkgs/cudatoolkit-10.2.89-hfd86e86_1.condaContent-Length: 382791771downloaded bytes: 310247084

原因

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

解决方法

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

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

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

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

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

url: https://mirrors.tuna./anaconda/pkgs/main/linux-64/cudatoolkit-10.2.89-hfd86e86_1.condatarget_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/pytorch160registry file: /home/hejian/.conda/environments.txt done

原因

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

解决方法

更改文件权限

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

参考资料

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

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

报错信息打印

hejian@dell:~$ conda activate pytorch1.6.0CommandNotFoundError: 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- powershellSee '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常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。