更新所有不来自conda的pip包

时间:2015-10-17 14:24:31

标签: python windows pip anaconda conda

在我的Windows 7系统上,我尝试使用conda安装尽可能多的软件包。这些很容易用

更新
conda update all

不幸的是,有些软件包不会出现在conda中,但可以通过pip获得,因此对于那些使用pip安装它们的软件包。更新Windows上的所有pip包似乎更难,但

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

是我发现的一种方式。

但是,这会尝试更新所有软件包,即使是我认为由conda安装的软件包。

有没有办法只更新pip安装的那些软件包?

3 个答案:

答案 0 :(得分:3)

以下是我尝试解析conda env export输出并升级任何PIP包的shell脚本:

#!/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 :(得分:1)

这很棘手,因为Pip包与conda包不同。 Anaconda将pip添加为安装选项并将它们放在环境中,但它不管理它们。 Pip仍然没有一个简单的命令来升级所有,但一些建议是你尝试过,这是另一个:

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

答案 2 :(得分:1)

这是另一个简单的脚本,使用conda list的输出,其中包含pip软件包列表。

conda list | grep "<pip>" | cut -d " " -f 1 | xargs pip install --upgrade