Simd
Simd

Reputation: 21343

Update all pip packages that don't come from conda

On my windows 7 system I try to install as many packages using conda. These are easy to update with

conda update all

Unfortunately some packages don't appear in conda but are available through pip and so for those I install them using pip. Updating all pip packages on windows seems more difficult but

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

is one way I found.

However, this attempts to update all packages, even those installed by conda I believe.

Is there some way to update only those packages installed by pip?

Upvotes: 8

Views: 4566

Answers (4)

omasoud
omasoud

Reputation: 479

This script (currently Windows only) intersects pip packages reported by conda with pip --outdated output and prints a result like 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

Upvotes: 1

Ohgyun Ahn
Ohgyun Ahn

Reputation: 787

This is an another simple script using output of conda list which contains list of pip packages.

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

Upvotes: 9

Marijn van Vliet
Marijn van Vliet

Reputation: 5409

Here is my attempt at a shell script that will parse the output of conda env export and upgrade any PIP packages:

#!/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 <[email protected]>
#
# 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

Upvotes: 3

John Morrison
John Morrison

Reputation: 4078

This is tricky as Pip packages are different than conda packages. Anaconda adds pip as an install choice and puts them in the environment but it does not manage them. Pip is still without an easy command to upgrade all but some suggestions are as you tried and this is another:

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

Upvotes: 1

Related Questions