Python | 如何使用pip升级所有Python软件包?

While using Python as a programming language, it's a very common scenario to use a virtual environment and PIP, a package manager for python.

当使用Python作为编程语言时,使用虚拟环境PIP (Python的程序包管理器)是一种非常常见的情况。

It's a common practice to use a text file, named as "requirement.txt", which would be populated with the list of libraries used in the given application.

通常,使用文本文件“ requirement.txt” ,其中将填充给定应用程序中使用的库的列表。

Generally, the developers maintain the version of the libraries in the "requirement.txt", as shown in the below example,

通常,开发人员在“ requirement.txt”中维护库的版本,如以下示例所示,

    (venv) XXX:src XXX$ more requirements.txt 
    numpy==1.17.2
    requirements.txt (END)

Upgrading every library is a monotonous task, and hence the following commands can be used to upgrade all the packages in the venv (virtual environment) using PIP. We could either follow the below as a two-step process or also combine it to be a single line command.

升级每个库都是一项单调的任务,因此,以下命令可用于使用PIP 升级 venv(虚拟环境)中的所有软件包 。 我们可以按照以下两个步骤进行操作,也可以将其组合为一个单行命令。

Approach 1:

方法1:

  1. Freeze all the libraries to a file called 'requirements.txt' (file name could be anything)

    将所有库冻结到一个名为“ requirements.txt”的文件中(文件名可以是任何名称)

    pip freeze > installed_library_list.txt
    
    
  2. Update all the libraries available in the file

    更新文件中所有可用的库

    pip install -r installed_library_list.txt –upgrade
    
    

Approach 2:

方法二:

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

The grep is to skip editable ("-e") package definitions, and the -n1 flag for xargs prevents stopping everything if updating one package fails.

grep将跳过可编辑的( “ -e” )软件包定义,并且xargs-n1标志可防止在更新一个软件包失败时停止所有操作。

翻译自: https://www.includehelp.com/python/upgrade-all-python-packages-with-pip.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值