anaconda初使用
前言
好久没有更新博客,上一篇博客还是数个月前写的,那么这么长的时间都在做什么呢?2020年受到新冠的影响,许许多多的工作都被延迟了,整天只能呆在家里,尽量不给大家添麻烦。虽然身体被拘束在家中但是头脑里总是想做点什么,想起了大学期间上操作系统这门课时,我们做实验使用的是老师自制的类Linux操作系统(源码地址:https://github.com/hongmingjian/epos),心血来潮想要自己写一个操作系统,一方面是为了打发时间,另一方面则是为了熟悉操作系统的运行原理。截至目前为止,操作系统完成了一部分,源码地址:https://github.com/qzt8315/QXOS。好了,我们回到正题上,六月份开始到支教结束这段时间,准备暂时缓一缓自制操作系统的工作,准备好好学习一下tensorflow,为之后入学研究生,开始学习深度学习准备。电脑上使用的最好方式就是使用python中的tensorflow库。之前一直都在使用anaconda,因为可以使用conda(包管理器)安装新库,并且集成了常用的科学计算的库,但是很少去学习anaconda对python环境(和virtualenv类似的功能)和包的管理。那么这篇博客中便介绍anaconda的一些使用技巧。
安装anaconda
这次使用window10操作系统,安装的版本是:Anaconda3-2019.10-Windows-x86_64,这里由于国内网络的环境,使用的是清华镜像网站链接下载。(也可以安装anaconda的mini安装包,地址Miniconda3-4.5.1-Windows-x86_64,只有几十兆大小,不会自带安装众多的包)
安装过程比较简单,除了安装路径和勾选将anaconda添加加到PATH环境变量(PS:强烈建议添加,避免后面手动添加)需要根据自己的需求选择外,其他默认安装即可。
anaconda添加到环境变量
将安装路径、安装路径\Scripts、安装路径\Library\mingw-w64\bin和安装路径\Library\bin添加到环境变量。
包管理
更改镜像地址
一般安装的Anaconda默认的源地址在国外,安装时会比较慢,可以修改为国内镜像源地址加快下载安装速度。一般国内源使用清华大学的镜像。在Windows上修改C:\Users\当前用户名\.condarc(Linux或者Mac修改~/.condarc):
ssl_verify: true
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- defaults
show_channel_urls: true
也可以使用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/
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/conda-forge/
conda config --set show_channel_urls yes
如果修改源之后conda下载速度还是比较慢,则可以修改pip源,使用pip安装包,Windows下修改C:\Users\当前用户名\pip\pip.ini(MaxOS或者Linux修改~/.pip.conf):
[global]
trust-host = pypi.douban.com
index-url = http://pypi.douban.com/simple
安装包
使用conda安装库与pip类似,使用命令:
# 搜索包
conda search 包名
# 安装包
conda install 包名
# 安装指定包
conda install 包名=1.2(版本)
# 安装多个包
conda install 包名1 包名2 ...
查看包
conda查看包列表:
conda list
# 查看特定环境的所有的包
conda list -n 环境名
更新包
anaconda包管理中,本身可以看作是一个包,python环境可以看作一个包,anaconda也可以看作一个包,因此在conda中除了支持普通的更新第三方包以外,还支持conda、python环境和anaconda更新。
# 更新包
conda update 包名
# 更新python
conda update python
# 更新anaconda
conda update anaconda
# 更新conda
conda update conda
# 防止包更新
conda update 包名 --no-pin
卸载包
使用conda卸载包和使用pip卸载包(uninstall)有区别:
# 卸载当前环境中的包
conda remove 包名
# 卸载特定环境中的包
conda remove -n 环境名 包名
# 删除多个包
conda remove 包名1 包名2 ...
python环境管理
之所以有环境管理是因为,可能会使用不同版本的python、不同版本的库等等的需求,那么相对独立的python运行环境就比较重要。
控制环境
创建环境
使用conda创建环境,类似于virtualenv的功能:
# 创建特定名字的环境
conda create -n 环境名
# 使用特定版本的python创建环境
conda create -n 环境名 python=版本(1.2)
# 使用特定包创建环境
conda create -n 环境名 包名
# 使用环境配置文件(.yaml)创建环境
conda env create -f 环境配置文件.yaml
环境配置文件可以通过导出生成
# 激活环境
conda activate 需要导出的环境名
# 导出
conda env export > 环境配置文件.yaml
激活环境
conda activate 环境名
停用环境
conda deactivate 环境名
查看当前环境
conda info -envs
删除环境
conda remove --n 环境名 --all
构建相同的环境
激活需要导出配置文件的环境
conda list --explicit > 文件.txt
在同系统的不同机器执行
conda create --name 环境名 -f 文件.txt
克隆环境(同一机器)
conda create --name 环境名 --clone 被克隆环境名
渠道管理
添加到顶部最高优先级
conda config --add channels 新渠道
# 或者
conda config --prepend 新渠道
添加到底部最低优先级
conda config --append channels 新渠道
开启搜索时显示通道地址
conda config --set show_channel_urls yes
删除源
# 删除所有新添加源恢复默认源
conda config --remove-key channels
常见的国内源
# 清华源
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
# 中科大源
https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
# 上交源
https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge/
设置pip源
临时设置pip源
pip install 库名 -i pip源网址
永久设置pip源,修改C:\Users\用户\pip\pip.ini配置文件(不存在时,可以手动创建配置文件),在linux下配置文件为~/.pip/pip.conf。
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
trusted-host = pypi.tuna.tsinghua.edu.cn
其他pip源
- 阿里云 http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣 http://pypi.douban.com/simple/
- 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
- 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
- 华中理工大学 http://pypi.hustunique.com/simple/
- 山东理工大学 http://pypi.sdutlinux.org/simple/
常用的镜像网站:国内镜像网站列表
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 qinzhtao@163.com
文章标题:anaconda初使用
文章字数:1.7k
本文作者:捌叁壹伍
发布时间:2020-06-04, 19:04:19
最后更新:2020-06-09, 00:16:51
原始链接:http://qzt8315.github.io/2020/06/04/anaconda初使用/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。