If you are anything like me, you use Python extensively for a wide range of different tasks. My main usage for Python is through Ansible, a tool I use often. It’s an invaluable tool which I simply could not be without.
On August 29th, Ubuntu finally released 24.04.01 LTS, which meant end users could now officially upgrade from Ubuntu 22.04 LTS with official tools. I have quite a few test servers running 22.04, and I have now slowly started the process of upgrading them all to 24.04.
With the upgrade to 24.04, this also meant that Python was upgraded from Python 3.10 to Python 3.12. I wanted a simple method to upgrade all my user-installed Python packages on the newly upgraded servers. Luckily this is quite simply using a one-liner which you find below.
A high-level walkthrough of what this does:
- Uses pip to generate a list of installed modules for Python 3.10 and stores them in
/tmp/py-packs.txt
- Then we use
sed
to perform some inline replacements- First remove the two first lines, which contain header information
- Then replace all reoccurring spaces with
==
which is the separator used for Python requirement files
- We then use pip3 to install all the packages found in the requirements file
/tmp/py-packs.txt
- After the packages are installed, we remove the generated requirements file
python3 -m pip list --path ~/.local/lib/python3.10/site-packages/ > /tmp/py-packs.txt; sed -i '1,2d; s/\s\+/ == /g' /tmp/py-packs.txt; pip3 install --force-reinstall -r /tmp/py-packs.txt; rm /tmp/py-packs.txt
Additional information
You will likely have a file called EXTERNALLY-MANAGED
in the /usr/lib/python3.12/
folder – this needs to be removed for you to be able to install/upgrade packages. To do so, perform the following:
sudo rm /usr/lib/python3.12/EXTERNALLY-MANAGED
After you have completed installing/upgrading all your Python 3.10 packages to Python 3.12, you can now remove the old 3.10 ones by performing the following command:
rm -rf ~/.local/lib/python3.10