1-数据分析 - anaconda环境配置及使用

about

win10 + anaconda

下载

有了anaconda,还有安装Python么?
anaconda是集成了Python和预装了科学计算的库和其他的第三方库,且集成了包管理工具conda。
一般的有了anaconda,就不用再装Python了,同时也支持多种发行版本共存。
anaconda有anaconda2和anaconda3分别对应Python2和Python3。

安装

安装过程
  1. 以管理员权限运行exe文件,然后next

  1. I agree

  1. 为所有用户安装

  1. 安装位置不允许有中文、空格等特殊字符,且记住安装位置

  1. 高级配置,按图操作

  1. 短暂的安装过程,完事提示安装成功

在开始栏就安装了这些:

注意,安装位置是这里:C:\ProgramData\Anaconda3,注意,如果你在C盘根目录没有发现ProgramData目录的话,说明这个目录被隐藏了,你点击"查看",勾选"隐藏项目"就显示出来了。

配置系统变量
注意是系统变量,而不是用户变量。我这里anaconda安装在C:\ProgramData

系统变量 路径 说明
Path C:\ProgramData\Anaconda3 Python需要
Path C:\ProgramData\Anaconda3\Scripts conda自带脚本
Path C:\ProgramData\Anaconda3\Library\bin jupyter notebook动态库
Path C:\ProgramData\Anaconda3\Library\mingw-w64\bin 使用C with python的时候
Path C:\ProgramData\Anaconda3\Library\usr\bin 没有该目录就算了

由于版本不一致,可能有些目录不存在,没有就算了。

测试是否安装成功

终端中输入如下几个命令,正常返回说明安装成功,否则检查添加path是否成功,或者重启系统后尝试:

python -V
conda info
conda --version

配置国内镜像源

一些第三方的包的官网都在国外,导致下载太慢或者无法下载。这里提供配置国内的镜像源:

# 添加清华源
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 http://mirrors.aliyun.com/pypi/simple/


# 设置搜索时显示通道地址
conda config --set show_channel_urls yes

查看是否修改好通道?
conda config --show channels

如果发现说某个channel url报404,那么就按照博客后面的处理办法恢复默认就行了。

虚拟环境管理

# 查看conda的配置
conda config --show

# 修改配置
conda config --add key value
conda config --remove key value

# 检查更新当前的conda管理工具
conda update -n base -c defaults conda -y

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

# 创建虚拟环境,指定虚拟环境的Python版本,不指定的话,默认跟anaconda的默认Python解释器版本
# 注意,使用conda config --show查看envs_dirs指向的路径,该路径是虚拟环境的默认安装位置,你也可以修改这个值
conda create -n 虚拟环境名称 python=3.6 -y

# 修改虚拟环境的保存位置,默认是保存到了用户家目录下
conda config --add envs_dirs C:\ProgramData\Anaconda3\envs

# 激活/退出虚拟环境
conda activate 虚拟环境名称
conda deactivate

# 为指定虚拟环境安装指定包,删除指定包;当然,也可以在激活虚拟环境后,使用pip下载,他俩的区别:conda可以不激活虚拟环境,就可以为指定虚拟环境安装包,而pip需要激活对应的虚拟环境才能安装包
conda install -n 虚拟环境名称 包名 -y
conda remove -n 虚拟环境名称 包名 -y

# 查看所有虚拟环境
conda info --envs
conda info -e
conda env list

# 删除指定虚拟环境
conda remove -n 虚拟环境名称 --all -y

使用

# 搜索可用的包
anaconda search -t conda requests

# 如果你不知道使用命令进行安装,执行下面命令来获取安装命令
anaconda show requests

这里举个例子,我有个虚拟环境py39中使用正常的方式无法下载dbutils模块,显示这样的结果:

C:\Users\Anthony>conda install dbutils -n py39 -y
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - dbutils

Current channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

这下只能搜索其他可用的包了:

C:\Users\Anthony>anaconda search -t conda dbutils      # 搜索可用的包
Using Anaconda API: https://api.anaconda.org
Packages:
     Name                      |  Version | Package Types   | Platforms       | Builds
     ------------------------- |   ------ | --------------- | --------------- | ----------
     CEFCA/dbutils             |      1.1 | conda           | linux-64, linux-32, osx-64 | py27_0
                                          : Database connections for multi-threaded environments.
     auto/dbutils              |      1.1 | conda           | linux-64, linux-32 | py27_0
                                          : http://www.webwareforpython.org/DBUtils
     conda-forge/gmpyinfr_dbutils |    0.0.7 | conda           | noarch          | pyhd8ed1ab_0
                                          : Infraestrutura dbutils python de uma equipe de Ciência de Dados
     mcrot/dbutils             |      1.1 | conda           | win-32          | py27_0
                                          : Database connections for multi-threaded environments.
     qian_bi/dbutils           |      1.3 | conda           | noarch          | py_0
                                          : Database connections for multi-threaded environments.
Found 5 packages

Run 'anaconda show <USER/PACKAGE>' to get installation details

C:\Users\Anthony>anaconda show qian_bi/dbutils      # 这一步就是一个个试,找下载方式
Using Anaconda API: https://api.anaconda.org
Name:    dbutils
Summary: Database connections for multi-threaded environments.
Access:  public
Package Types:  conda
Versions:
   + 1.3

To install this package with conda run:
     conda install --channel https://conda.anaconda.org/qian_bi dbutils

开始下载吧:

C:\Users\Anthony>conda install --channel https://conda.anaconda.org/qian_bi dbutils -n py39 -y
.........
done

常见报错

UnavailableInvalidChannel: The channel is not accessible or is invalid.

win10 +

我准备创建个虚拟环境,发现报错:

(base) D:\tmp\demo>conda create -n dj1 python=3.9 -y
Collecting package metadata (current_repodata.json): failed

UnavailableInvalidChannel: The channel is not accessible or is invalid.
  channel name: simple
  channel url: https://pypi.tuna.tsinghua.edu.cn/simple
  error code: 404

You will need to adjust your conda configuration to proceed.
Use `conda config --show channels` to view your configuration's current state,
and use `conda config --show-sources` to view config file locations.

经查,说是什么镜像未经授权,咱也不懂,但解决办法就是需要恢复到官方源镜像,恢复镜像命令:

conda config --remove-key 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

The environment is inconsistent, please check the package plan carefully

win10

使用conda安装pymysql,发现安装失败。

(base) D:\downloads\demo>conda install pymysql
Collecting package metadata (current_repodata.json): done
Solving environment: |
The environment is inconsistent, please check the package plan carefully
The following packages are causing the inconsistency:

  - defaults/win-64::anaconda==2021.11=py39_0
  - defaults/win-64::anaconda-client==1.9.0=py39haa95532_0
  - defaults/win-64::anaconda-navigator==2.1.1=py39_0

经查,以管理员的权限打开cmd终端,依次执行下面三个命令(耗时非常久,耐心等待,也有可能会失败,多尝试两次):

conda activate base
conda install anaconda
conda update --all

参考:


that's all,see also:

【Anaconda教程01】怎么安装Anaconda3 | Anaconda3详细安装使用教程及问题总结 | Anaconda多版本Python管理以及TensorFlow版本的选择安装 | jupyter notebook win10设置启动路径,并配置远程访问,开机自启动不弹窗 | Python 技术篇-设置windows开机自动启用Jupyter服务,BAT批处理脚本启用jupyter服务设置,设置jupyter默认启动位置的方法 | conda虚拟环境 | anaconda安装不存在的包

posted @ 2018-09-02 18:37  听雨危楼  阅读(1338)  评论(1编辑  收藏  举报