Upgrading TLJH using a script file

There have been numerous upgrades to python and TLJH recently. My IT guy wanted a script to make upgrading easier so I made one for him.

#! /bin/sh
sudo -E -H pip list --outdated --format=freeze | sed ‘s/==/>=/g’ | sudo -E -H xargs pip install --upgrade
pip check

We saved this as tljh-upgrade

The first part
sudo -E -H pip list --outdated --format=freeze
make a list of packages that must be upgraded in freeze format which shows the current version of each package
sed ‘s/==/>=/g’
Replaces all the == with >= so the packages will look for upgraded packages.
The last part
sudo -E -H xargs pip install --upgrade
does the actual upgrade. xargs takes the out put of the second part and appends it to the command line for the pip install --upgrade.
We have tested it and it works.

We out this in a bash script and did a chmod +x tljh-upgrade on the script file to it executable.

This script file can also be used just to upgrade python but the -E -H after the sudo isn’t necessary and it may be safer to add --user after the --upgrade.

1 Like

What I have learned.
Upgrading jupyter lab locally is best done using
for linux
python -m pip list --outdated --user --format=freeze | sed ‘s/==/?=/g’ | xargs python -m pip install --upgrade
The first part list only the outdated user files. System files should be updated by apt or whatever package manager you use.
The second part replaces the == in the piped data with >=
Windows doesn’t have a sed so this doesn’t work in windows.
The third part appends the edited piped data to the end of the command after the --upgrade and does the final upgrade.
I have made this an executable bash file.
Windows does not have a sed command so I take a more brute force approach.
I start with
python -m pip freeze > requirements.txt --user
This is like what is posted on the Active State web site but they don’t include the --user which is important.
This lists all user packages and I save this file in my home directory.
Every once in a while I check for out of date packages and execute
python -m pip install —upgrade -r requirements.txt
This brute force approach will check each package to see if it needs to be updated and update those that do.
The brute force approach works on OS X, Windows and Linux.
I use python -m pip instead of just pip because I get warnings about pip using an old wrapper script.

The TLJH updates require the sudo -E in front of the python -m pip …
This is listed here
https://tljh.jupyter.org/en/latest/install/custom-server.html
It is best to automate this.

I was told never to use sudo before pip. This must be an exception because the IT guy wants to install the packages for all users so each users does’t need to install or upgrade packages themselves.

This is the script I use to update my python installation on windows. I know this has nothing to do with tljh but I think many can make use of it.

I use power shell. Sometimes packages cannot be upgraded to the latest version because of restriction from another package. This is the case with idna so I had to install the version that works after updating all the other files.

pip list --outdated --user --format=freeze > req0.txt
$content = Get-Content -Path ‘req0.txt’
$newContent = $content -replace ‘==’, ‘>=’
$newContent | Set-Content -Path ‘req1.txt’
pip install --user --upgrade -r req1.txt
pip install --user idna==2.8 --user
pip list --outdated
pip check