Orion42 Blog

~IT made easy~

Linux, Bash and Git

[Shell] How to check if pip/pip3 packages need updating

Today I present a small shell script that check if pip/ pip3 are installed and if there is any package that need to be update:

printf "Check PIP and PIP3 versions and packages: \n\n "

if [[ $(command -v pip) ]] ; then 
    printf "PIP found: \n  "
    pip --version ; 
    pip_pkg=$( pip list --outdated --format=freeze | wc -l)
    printf "  Pakages to be upgraded: $pip_pkg \n"
fi

if [[ $(command -v pip3) ]] ; then 
    printf "PIP3 found: \n  "
    pip3 --version ; 
    pip3_pkg=$( pip list --outdated --format=freeze | wc -l)
    printf "  Pakages to be upgraded: $pip3_pkg \n"
fi

Since the steps are the same for both pip and pip3, I will describe just once.

  • The if block check if pip is installed. If so the version is printed.
  • Then a variable is set to contain the number of packages that need updating. This command is the sum of two instructions:
    • pip list --outdated --format=freeze lists the outdated packages (with the number of the current version installed)
    • wc -l counts the number of affected rows.

In case you want to display only the name of the outdated packages, you just need to run:

  • pip list --outdated --format=freeze | awk '{split($0, a, "=="); print a[1]}'

 

Leave a Reply

Skip to content