更新所有不来自conda的pip软件包

7

在我的Windows 7系统上,我尝试使用conda安装尽可能多的软件包。这些软件包可以很容易地通过以下方式更新:

conda update all

很不幸,有些软件包在conda中并没有出现,但是可以通过pip获取。对于这些软件包,我会使用pip进行安装。在Windows上更新所有pip软件包似乎更加困难,但是...

for /F "delims===" %i in ('pip freeze -l') do pip install -U %i

我发现有一种方法可以更新Python的包。

然而,这个方法尝试着更新全部的包,包括那些通过conda安装的包。

有没有办法只更新通过pip安装的包呢?

4个回答

9
这是另一个简单的脚本,使用conda list的输出内容,其中包含了pip软件包的列表。
conda list | grep "<pip>" | cut -d " " -f 1 | xargs pip install --upgrade

3
这对我有用:conda list | grep "pypi" | cut -d " " -f 1 | xargs pip install --upgrade - robbwh

3

这是我尝试编写的一个shell脚本,它可以解析conda env export命令的输出,并升级任何PIP包:

#!/bin/sh

###############################################################################
# Script to scan an Anaconda environment and upgrade any PIP packages.
#
# Usage:
# $ ./upgrade_pip_packages.sh 
# or explicitly give it an environment file to parse:
# $ ./upgrade_pip_packages.sh <environment.yml file>
#
# Author: Marijn van Vliet <w.m.vanvliet@gmail.com>
#
# Version: 1.0 (29-09-2017)
#   - Initial version of the script.

# Check for optional command line argument
if [ "$#" = 0 ]
then
    ENV_OUTPUT=`conda env export`
elif [ "$#" = 1 ]
then
    ENV_OUTPUT=`cat $1`
else
    echo "Usage: $0 [environment file]" >&2
    exit 1
fi

PIP=0  # Whether we are parsing PIP packages
IFS=$'\n'  # Split on newlines
PIP_PACKAGES=""  # PIP packages found thus far

# Loop over the output of "conda env export"
for line in $ENV_OUTPUT
do
    # Don't do anything until we get to the packages installed by PIP
    if [ "$line" = "- pip:" ]
    then
        PIP=1  # From this point, start doing things.
    elif [[ "$line" = prefix:* ]]
    then
        PIP=0  # End of PIP package list. Stop doing things.
    elif [ $PIP = 1 ]
    then
        # Packages are listed as "   - name==version==python_version"
        # This is a regular expression that matches only the name and 
        # strips quotes in git URLs:
        REGEXP='^  - "\?\([^="]*\)"\?.*$'

        # Find PIP package name (or git URL)
        PIP_PACKAGES="$PIP_PACKAGES `echo "$line" | sed -n "s/$REGEXP/\1/p"`"
    fi
done

# From now on, split on spaces
IFS=' '

echo "The following packages are marked for upgrade using PIP:"
echo
for package in $PIP_PACKAGES
do
    echo " - $package"
done
echo

read -r -p "Shall we proceed with the upgrade? [y/N] " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        # Upgrade each package
        for package in $PIP_PACKAGES
        do
            pip install --upgrade $package
        done
        ;;
    *)
        echo "Aborting"
        ;;
esac

1
这个脚本(目前仅适用于Windows)将conda报告的pip软件包与pip --outdated的输出进行交集运算,并打印出如下结果:

This


C:\>python.exe pip_updates.py --env base

Querying conda to get pip packages...
Querying pip to get outdated packages...

There are a total of 49 pip (non-conda) packages.
pip reports there are 25 packages that are out of date.
Of those, the non-conda ones are: 6.

Here are the current and latest versions for these 6:
cachetools:         4.2.4 -> 5.0.0
gast:               0.4.0 -> 0.5.3
pylibjpeg:          1.3.0 -> 1.4.0
pylibjpeg-libjpeg:  1.2.0 -> 1.3.0
pylibjpeg-openjpeg: 1.1.1 -> 1.2.0
pylibjpeg-rle:      1.2.0 -> 1.3.0

And here are the pip update commands for those:

pip install cachetools --upgrade
pip install gast --upgrade
pip install pylibjpeg --upgrade
pip install pylibjpeg-libjpeg --upgrade
pip install pylibjpeg-openjpeg --upgrade
pip install pylibjpeg-rle --upgrade

1
这很棘手,因为Pip包与conda包不同。Anaconda将pip作为安装选择并将其放置在环境中,但它不管理它们。Pip仍然没有一个简单的命令来升级所有的包,但有一些建议,就像你尝试过的一样,这是另一个建议:
pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs pip install -U

谢谢你的回答,但是它在Windows上不起作用,而且也不能防止pip升级conda正在管理的软件包,对吗? - Simd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接