Skip to content

Python Upgrade Steps from version 3.7 to 3.12

1. Introduction

This document provides detailed steps to upgrade Python from version 3.7 to 3.12 on an on-prem Registry/CLI VM. The process includes scripts for upgrading, checking the Python version, and troubleshooting potential issues after the upgrade.

2. Prerequisites

  • Ensure that you have internet connectivity for the installation of Python 3.12 (use offline steps if not).

  • The below steps must be executed on the on-prem Registry/CLI VM only.

  • Backup your existing environment, configurations, and dependencies before proceeding.

3. Installing Python 3.12

  • Step 1: Download the script for upgrading Python 3.12 Execute one of the following commands based on your internet availability:

wget https://macaw-amer.s3.us-east-1.amazonaws.com/releases/rdaf-platform/1.4.0/install_python3.12-internet.sh
* Step 2: Execute the Installation Script

Please use the steps given below to install python version 3.12 with internet connectivity

sh install_python3.12-internet.sh
sh install_python3.12-internet.sh
Starting Python 3.12 installation...
Adding deadsnakes PPA...
Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [128 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease [18.1 kB]
Hit:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 Packages [29.6 kB]
Hit:6 http://us.archive.ubuntu.com/ubuntu focal-security InRelease
Get:7 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main Translation-en [5,892 B]
Fetched 181 kB in 1s (199 kB/s)
Reading package lists... Done
Updating package lists...
Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu focal-security InRelease
Hit:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
249 packages can be upgraded. Run 'apt list --upgradable' to see them.
Installing Python 3.12...
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libevent-core-2.1-7 libevent-pthreads-2.1-7 libopts25 linux-headers-5.4.0-109 linux-headers-5.4.0-109-generic linux-image-5.4.0-109-generic linux-modules-5.4.0-109-generic
linux-modules-extra-5.4.0-109-generic sntp
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libpython3.12-stdlib
Suggested packages:
python3.12-venv
The following NEW packages will be installed:
libpython3.12-stdlib python3.12
0 upgraded, 2 newly installed, 0 to remove and 249 not upgraded.
Need to get 5,268 kB of archives.
After this operation, 23.2 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.12-stdlib amd64 3.12.9-1+focal1 [2,755 kB]
Get:2 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.12 amd64 3.12.9-1+focal1 [2,513 kB]
Fetched 5,268 kB in 12s (455 kB/s)
Selecting previously unselected package libpython3.12-stdlib:amd64.
(Reading database ... 153289 files and directories currently installed.)
Preparing to unpack .../libpython3.12-stdlib_3.12.9-1+focal1_amd64.deb ...
Unpacking libpython3.12-stdlib:amd64 (3.12.9-1+focal1) ...
Selecting previously unselected package python3.12.
Preparing to unpack .../python3.12_3.12.9-1+focal1_amd64.deb ...
Unpacking python3.12 (3.12.9-1+focal1) ...
Setting up libpython3.12-stdlib:amd64 (3.12.9-1+focal1) ...
Setting up python3.12 (3.12.9-1+focal1) ...
Preparing to unpack .../python3.12-venv_3.12.9-1+focal1_amd64.deb ...
Unpacking python3.12-venv (3.12.9-1+focal1) ...
Setting up python3.12-venv (3.12.9-1+focal1) ...
Creating a virtual environment at /opt/PYTHON3...
Checking Python version...
Python 3.12.9
Python 3.12 installation completed successfully.
  • Step 3: Verify the Installation
pip3 --version
pip 24.2 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)

wget https://macaw-amer.s3.us-east-1.amazonaws.com/releases/rdaf-platform/1.4.0/install_python3.12_withoutinternet.sh
* Step 2: Execute the Installation Script

Please use the steps given below to install python version 3.12 without internet connectivity

sh install_python3.12_withoutinternet.sh
  • Step 3: Verify the Installation
pip3 --version
pip 24.2 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)

4. Fixing PIP Version Issue

Note

If pip still shows as version 3.7, execute the following commands to correct the issue.

Navigate to the local library directory using below command

cd ~/.local/lib/
Rename the Python 3.7 folder

mv python3.7/ python3.7_old
Navigate to the bin directory using below command

cd ~/.local/bin

Rename the existing pip and pip3 binaries

mv pip pip.old
mv pip3 pip3.old

Create symbolic links to the new pip version using commands given below

ln -s /usr/local/bin/pip3.12 pip
ln -s /usr/local/bin/pip3.12 pip3
  • Step 4: After following the commands given above, Please check python version using below given command , it should be 3.12.8
python --version
python --version
python 3.12.8

5. Troubleshooting Post Upgrade Issues

Note

After upgrading, if the user encounters any errors similar to the ones listed below, then please follow the below given steps to resolve.

Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
  • Step 1: Check for the Missing Module, check if the apt_pkg module is installed in Python 3 environment, run the following command listed below
ls /usr/lib/python3/dist-packages | grep apt_pkg
apt_pkg-stubs
apt_pkg.cpython-38-x86_64-linux-gnu.so
  • Step 2: Create a symbolic link for apt_pkg version
sudo ln -s  /usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so  /usr/lib/python3/dist-packages/apt_pkg.so
$ sudo ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
  • Step 3: Verify the Import

  • Goto the path /usr/lib/python3/dist-packages and execute the below command

rdauser@<HOSTNAME>:/usr/lib/python3/dist-packages$ python3 -c "import apt_pkg"

rdauser@platform-32:~$ cd /usr/lib/python3/dist-packages
rdauser@platform-32:/usr/lib/python3/dist-packages$ python3 -c "import apt_pkg" 
rdauser@platform-32:/usr/lib/python3/dist-packages$
Step 4: Reinstall Missing Packages

If the above steps does not resolve the issue, reinstall the necessary packages

a) uninstall and reinstall the python-debian package

  • To uninstall the python-debian package using pip, please use the below mentioned command
sudo pip uninstall python-debian -y
  • To reinstall the python-debian package using pip, please use the below mentioned command
pip install python-debian
$ sudo pip uninstall python-debian -y
Found existing installation: python-debian 0.1.36ubuntu1
Uninstalling python-debian-0.1.36ubuntu1:
    Successfully uninstalled python-debian-0.1.36ubuntu1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unu sable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.

$ pip install python-debian
Defaulting to user installation because normal site-packages is not writeable
Collecting python-debian
    Downloading python_debian-0.1.49-py3-none-any.whl.metadata (1.2 kB)
Requirement already satisfied: chardet in /usr/lib/python3/dist-packages (from python-debian) (3.0.4)
Downloading python_debian-0.1.49-py3-none-any.whl (132 kB)
WARNING: Error parsing dependencies of distro-info: Invalid version: '0.23ubuntu1'
Installing collected packages: python-debian
Successfully installed python-debian-0.1.49

b) Similarly, uninstall and reinstall the distro-info package

  • This below mentioned command is used to uninstall the distro-info package using pip
sudo pip uninstall distro-info -y
  • Please use ths below mentioned command to reinstall the distro-info package via pip
sudo pip install distro-info
$ sudo pip uninstall distro-info -y 
Found existing installation: distro-info 0.23ubuntu1
Uninstalling distro-info-0.23ubuntu1:
    Successfully uninstalled distro-info-0.23ubuntu1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unu sable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.

$ pip install distro-info
Defaulting to user installation because normal site-packages is not writeable
Collecting distro-info
    Downloading distro_info-1.0-py3-none-any.whl.metadata (225 bytes)
Downloading distro_info-1.0-py3-none-any.whl (4.0 kB)
Installing collected packages: distro-info
Successfully installed distro-info-1.0
  • After following the commands given above, Please check python version using below given command , it should be 3.12.8
python --version
rdauser@rdafk8182:~$ python --version
python 3.12.8