Some methods about batch Update Python Packages.

替换pip源

Python经常需要使用pip install xxx安装各种包,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下。个人喜欢清华大学的pip源,它是官网pypi的镜像,每隔5分钟同步一次,Windows用户替换pip源方式:
进入你的C:\Users\SunQian\下,新建名为pip的文件夹,在文件夹下新建一个名为pip.ini,用文本方式打开并在里面写入并保存:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

Method 0

1. List of all outdated packages.

pip list  --outdated --format=legacy     
pip list  --outdated --format=columns   ##对比format=legacy,format=columns输出格式和表格类似。

2. Update one package

pip install -U xxxx (xxxx指包的名称,该方法的缺点是包需要一个一个更新)

Method 1

1. Get a list of all the outdated packages.

To generate a list of all outdated packages:

pip list --outdated

2.1 Update All Python Packages On Windows

Open a command shell by typing ‘powershell’ in the Search Box of the Task bar

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}

2.2 Update All Python Packages On Linux

  • To upgrade all packages using pip with grep on Ubuntu Linux:

    pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U 
  • To upgrade all packages using pip with awk on Ubuntu Linux:

    pip3 list -o | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print)' | cut -d' ' -f1 | xargs -n1 pip3 install -U 

Method 2

Use pip-upgrade-all package. GitHub. pypi.org

1. Installation

pip install pip-upgrade-all

2. Usage

upgradeall.py [-h] [-l LIMIT] [-c] [-v]

Method 3

Use pip-review function.

pip install pip-review
pip-review --local --interactive

Reference

  1. How To Update All Python Packages
  2. How to Update All of Your Python Packages With pip Using One Simple Command
  3. Python更新所有已安装包的方法
  4. Python使用pip更新所有已安装包的方法