软硬件环境
- ubuntu 16.04 64bit
- Anaconda 3.5.1.0
- python 3.6
Anaconda是什么
Anaconda
是一个用于科学计算的 python
发行版,支持 Windows
, Linux
及 Mac
系统,提供了包管理的功能,可以非常方便的解决 python
的多版本并存,切换及各种第三方包的安装问题,并且还能独立于系统环境,添加及删除都非常方便。
安装配置
linux
版本的下载地址, https://www.anaconda.com/download/#linux,目前提供了分别基于 python2.7
和 python3.6
的2个正式版,最近 python
官方已经有消息称 python2
只会支持到2020年,所以如果没有特别的原因,请尽早迁移到 python3
。下载好的是一个脚本文件 Anaconda3-5.1.0-Linux-x86_64.sh
,然后执行
./Anaconda3-5.1.0-Linux-x86_64.sh
安装过程基本上是傻瓜式的,不过有3点需要注意,第一默认的安装路径在 /home/$user/anaconda3
,第二就是执行安装脚本时不要用 sudo
,这样做的好处是减少系统权限问题,管理方便,迁移方便,第三将 PATH
加入 ~/.bashrc
中,如果安装过程中不小心选择了 NO
,自行添加也可以,如下
export PATH=/home/xugaoxiang/anaconda3/bin:$PATH
conda命令
安装完 anaconda
,conda
命令就有了, 它是一个工具,可以在命令行中执行,与 pip
的使用非常类似,用来进行包管理及环境管理。
这里列几个 conda
的常用命令
# 查看当前环境下已安装的包
conda list
# 查看某个指定环境的已安装包
conda list -n python36
# 查找package信息
conda search numpy
# 安装package
conda install numpy
# 更新package
conda update numpy
# 删除package
conda remove numpy
# 更新conda, conda也是一个package
conda update conda
# 更新python
conda update python
# 创建虚拟环境
conda create -n test python=3.6
# 删除虚拟环境
conda remove -n test --all
# 查看所有创建的虚拟环境
conda env list
# 激活虚拟环境
conda activate test
# 退出虚拟环境
conda deactivate
# 虚拟环境克隆
conda create -n target --clone source
anaconda源管理
国内比较好的 anaconda
源,清华大学的,把它添加进来
编辑 ~/.condarc
文件
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- 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
然后执行命令 conda clean -i
清理索引缓存
pycharm中使用anaconda
依次打开 File --> Default Settings --> Project Interpreter
,新增本地的 python
解析器, 在出现的对话框中左侧选中 Conda Environment
, 对应的右侧 pycharm
会自动帮你找到 anaconda
中安装的 python
位置,你可以给它取个好记点的名字,如我这的 /home/xugaoxiang/anaconda3/envs/xugaoxiang
,版本号是3.6
,点击 Apply
,再选中 OK
完成配置。
在上述步骤结束之后,可以到 /home/xugaoxiang/anaconda3/envs/
目录下看看,在配置之前这个目录是空的,现在多处了很多的文件及目录,它是一个独立的虚拟环境
升级python
执行命令 conda update python
,升级完成后,发现执行 python
时报错
File "C:\Users\admin\anaconda3\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\Users\admin\anaconda3\lib\site-packages\urllib3\__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "C:\Users\admin\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 39, in <module>
from .response import HTTPResponse
File "C:\Users\admin\anaconda3\lib\site-packages\urllib3\response.py", line 155, in <module>
class HTTPResponse(io.IOBase):
File "C:\Users\admin\anaconda3\lib\site-packages\urllib3\response.py", line 377, in HTTPResponse
DECODER_ERROR_CLASSES += (brotli.error,)
AttributeError: module 'brotli' has no attribute 'error'
将 C:\Users\admin\anaconda3\lib\site-packages\urllib3\response.py
文件中的
if brotli is not None:
DECODER_ERROR_CLASSES += (brotli.error,)
改为
#if brotli is not None:
# DECODER_ERROR_CLASSES += (brotli.error,)
然后执行 conda update conda
,urllib3
软件包也随之升级,问题解决
虚拟环境中更改python版本
某些时候,已经创建好了对应的虚拟环境,也已经安装了部分依赖库,这时,发现 python
版本不兼容(比如需要3.7版本,而创建的环境是3.8),如果重新创建虚拟环境,就太麻烦了,可以这样
conda activate test
conda install python=3.7