更新 Python 库文件

Anaconda

查看库

Anaconda Navigator中启动Anaconda Prompt(或Anaconda Navigator中Environment->(base)root->Open terminal),输入 conda list 查看已经安装的库。
或者在Anaconda Navigator中Environment:选择

  • “installed”查看已经安装的包;(右键或左键)点击包名前的复选框,弹出的菜单中可选择“mark for update”升级该包,或选择“mark fo removal”移除该包。或在“Mark for specific version install”中选择安装哪种版本的包。选择菜单中的某项,点击右下角出现的"apply"(应用选择)或"clear"(清除选择)。
  • “Updatable”查看可更新的包。点击包名前的复选框,弹出菜单的解释与“installed”的情形类似。
  • "Not installed"查看要安装的库。
  • 要注意:出现的并不是选中的!没有绿色背景才是选中的情形!

安装或更新库

启动Anaconda Prompt,通过以下命令安装或更新库

conda install pandas   #安装pandas库
conda update pandas    #更新pandas库
conda update --all     #更新所有库,注意两个“-”号
conda list pandas      #查看pandas库的安装信息
conda list             #列出所有已经安装的库的安装信息

国内镜像网站:
https://pypi.mirrors.ustc.edu.cn (中国科技大学)
https://pypi.tuna.tsinghua.edu.cn (清华大学)
http://pypi.douban.com

Anaconda 中,可通过如下命令更改下载的源镜像(https://mirrors.tuna.tsinghua.edu.cn/help/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 --set show_channel_urls yes    # 显示通道地址

运行上面命令后,在%USERPROFILE%中生成配置文件.condarc。如果不想使用镜像,删除配置文件即可。

如果选择中科大作为源镜像,使用如下命令设置:

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/

conda config --set show_channel_urls yes

如果某个通道报错,删除该通道。

PS:貌似conda update --all更新过程中出现错误后,Anaconda Navigator显示并没有更新。但在Anaconda Navigator中的更新有效!

删除pkgs中的备份

目录pkgs中有已经安装的包的备份,conda clean -pconda clean -a清除备份的压缩包。

配置pip

打开%APPDATA%,新建目录pip,进入该目录,新建文件pip.ini,内容如下:

[global]  
index-url = http://pypi.douban.com
[install]
trusted-host = pypi.douban.com

设置CMD/命令行代理服务器

  1. 我的电脑->属性->高级->环境变量->系统变量。
  2. 新建参数名为:HTTP_proxy。
  3. 路径名为:http://代理:端口/

WinPython

WinPython中的包管理

配置pip同上。

列出所有第三方包:pip list
安装包:pip install 包名
安装包(指定源):pip install 包名 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
安装指定版本的包:pip install testpath==0.4.0
检测更新:pip list --outdated
升级包:pip install --upgrader 包名
卸载包:pip uninstall 包名
更新pip:python -m pip install --upgrade pip
降低pip版本:python -m pip install pip==9.0.3(将库中pip模块当作脚本运行,安装9.0.3)
查看指定包的历史版本pip install testpath==

批量更新过期的包(太慢,不推荐!):

import pip
from subprocess import call
from pip._internal.utils.misc import get_installed_distributions
for dist in get_installed_distributions():
    call("pip install --upgrade " + dist.project_name, shell=True)

注意:有些包依赖于低版本的包。

问题

使用pip list --outdated,前面几次没有提示问题。后来总是有警告

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.ustc.edu.cn', port=443): Read timed out. (read timeout=15)")': /pypi/web/simple/xarray/

解决pip.ini中的[global]index-url使用了https网址,去掉[install]段的内容。即pip.ini内容为:

[global]  
index-url=https://pypi.tuna.tsinghua.edu.cn/simple

更新包的错误提示

ERROR: datashader 0.6.9 has requirement testpath<0.4, but you'll have testpath 0.4.2 which is incompatible.

解决:卸载testpath,安装指定的低版本

原文地址:https://www.cnblogs.com/ourweiguan/p/10650869.html