When Python has its annual update, how to re-install your own packages

This info was incredibly helpful to me, but I wanted something slightly more automated, so I made a bit of a python/shell scripting, since I’m pretty novice to shell and couldn’t write it all in shell. (Note: this is for updating packages via PyPi only)

This is my pip-update.sh :

#! /bin/bash
# Change python version below from 3.10 to whatever your current version is

pip list --path $HOME/.local/lib/python3.10/site-packages/ > .pip-lst.txt

python3 Pip-update.py .pip-lst.txt

tr '\n' '\0' < .pip-adj.txt | xargs -r -0 pip install -U

rm -f .pip-lst.txt .pip-adj.txt

The output from pip list is manipulated via a python script that makes a new file with just the package names, one per line. The script then takes the lines and feeds them individually to pip install -U.

# Pip-update.py
import sys

file = open(sys.argv[1],'r')

vfile = []
for i in file:
    vfile.append(i)

file.close()
vfile = vfile[2:]
vfile_adj = []

for i in vfile:
    index = 0
    while (i[index] != ' '):
        index = index + 1
    vfile_adj.append(i[:index])

new_file = open('.pip-adj.txt','w')
for i in vfile_adj:
    new_file.writelines(i)
    new_file.writelines('\n')

new_file.close()

After saving both files, just run

sh pip-update.sh

Or whatever you named the shell script. Remember to rename the python script inside pip-update.sh in case of changes.

The only problem is that I couldn’t automate the picking of pathname given the current python version in the system, so it needs changing, e.g, from

$HOME/.local/lib/python3.10/site-packages/

To

$HOME/.local/lib/python3.11/site-packages/

When python 3.11 comes out. Of course, it shouldn’t be too hard to get this part of the code done, or to port it all to shell. But just in case someone finds it useful, I decided to post the code anyway.