Python packages no longer installing on jammy with python 3.10

As of yesterday, some python packages are no longer installing successfully on Jammy. For example, when installing psycopg2.

Some strange behaviour occurs with the preinstalled packages of pip and setuptools.

Attempting to upgrade pip, and setuptools prints that both packages are up-to-date. However, running pip --version reports that pip is on an older version.

Downgrading then upgrading pip, fixes the issue with the version of pip. But does not fix the issue with installing psycopg2.
However, removing setuptools and reinstalling it again fix the issue with the package installation.

Ps. Fixing setuptools and ignoring pip fixes the issue, but some packages install very slowly.
Pss. The issue installing psycopg2 is failing when importing a module from setuptools.

A detailed attempt showcasing the above can be found below.

$ pip install --no-cache-dir -U setuptools pip
Requirement already up-to-date: setuptools in /home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages (65.6.3)
Requirement already up-to-date: pip in /home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages (22.3.1)

$ pip --version
pip 20.1 from /home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/pip (python 3.10)

$ pip install pip==20.1
Collecting pip==20.1
  Downloading pip-20.1-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.3.1
    Uninstalling pip-22.3.1:
      Successfully uninstalled pip-22.3.1
Successfully installed pip-20.1

$ pip install -U pip
Collecting pip
  Downloading pip-22.3.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.1
    Uninstalling pip-20.1:
      Successfully uninstalled pip-20.1
Successfully installed pip-22.3.1

$ pip --version
pip 22.3.1 from /home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/pip (python 3.10)

$ pip install psycopg2==2.9.5 || true
Collecting psycopg2==2.9.5
  Downloading psycopg2-2.9.5.tar.gz (384 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [50 lines of output]
      running egg_info
      creating /tmp/pip-pip-egg-info-2zxygxc3/psycopg2.egg-info
      writing /tmp/pip-pip-egg-info-2zxygxc3/psycopg2.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-pip-egg-info-2zxygxc3/psycopg2.egg-info/dependency_links.txt
      writing top-level names to /tmp/pip-pip-egg-info-2zxygxc3/psycopg2.egg-info/top_level.txt
      writing manifest file '/tmp/pip-pip-egg-info-2zxygxc3/psycopg2.egg-info/SOURCES.txt'
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-akm_w6je/psycopg2_8a9c311d93aa4a6290faf9fdd3e20950/setup.py", line 550, in <module>
          setup(name="psycopg2",
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/__init__.py", line 144, in setup
          return distutils.core.setup(**attrs)
        File "/opt/python/3.10.8/lib/python3.10/distutils/core.py", line 148, in setup
          dist.run_commands()
        File "/opt/python/3.10.8/lib/python3.10/distutils/dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "/opt/python/3.10.8/lib/python3.10/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 297, in run
          self.find_sources()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 304, in find_sources
          mm.run()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 535, in run
          self.add_defaults()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 571, in add_defaults
          sdist.add_defaults(self)
        File "/opt/python/3.10.8/lib/python3.10/distutils/command/sdist.py", line 226, in add_defaults
          self._add_defaults_python()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/sdist.py", line 135, in _add_defaults_python
          build_py = self.get_finalized_command('build_py')
        File "/opt/python/3.10.8/lib/python3.10/distutils/cmd.py", line 299, in get_finalized_command
          cmd_obj.ensure_finalized()
        File "/opt/python/3.10.8/lib/python3.10/distutils/cmd.py", line 107, in ensure_finalized
          self.finalize_options()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/command/build_py.py", line 39, in finalize_options
          orig.build_py.finalize_options(self)
        File "/opt/python/3.10.8/lib/python3.10/distutils/command/build_py.py", line 43, in finalize_options
          self.set_undefined_options('build',
        File "/opt/python/3.10.8/lib/python3.10/distutils/cmd.py", line 286, in set_undefined_options
          src_cmd_obj = self.distribution.get_command_obj(src_cmd)
        File "/opt/python/3.10.8/lib/python3.10/distutils/dist.py", line 857, in get_command_obj
          klass = self.get_command_class(command)
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/setuptools/dist.py", line 764, in get_command_class
          self.cmdclass[command] = cmdclass = ep.load()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2450, in load
          return self.resolve()
        File "/home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2456, in resolve
          module = __import__(self.module_name, fromlist=['__name__'], level=0)
      ModuleNotFoundError: No module named 'setuptools.command.build'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

$ pip uninstall -y setuptools
Found existing installation: setuptools 65.6.3
Uninstalling setuptools-65.6.3:
  Successfully uninstalled setuptools-65.6.3

$ pip install -U setuptools
Requirement already satisfied: setuptools in /home/travis/virtualenv/python3.10.8/lib/python3.10/site-packages (46.1.3)
Collecting setuptools
  Downloading setuptools-65.6.3-py3-none-any.whl (1.2 MB)
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 46.1.3
    Uninstalling setuptools-46.1.3:
      Successfully uninstalled setuptools-46.1.3
Successfully installed setuptools-65.6.3

$ pip install psycopg2==2.9.5
Collecting psycopg2==2.9.5
  Using cached psycopg2-2.9.5.tar.gz (384 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... -\-/\/\|-|\|-\/\done
  Created wheel for psycopg2: filename=psycopg2-2.9.5-cp310-cp310-linux_x86_64.whl size=514965 sha256=3bd07ebd72dc1b9537527e85c0356a94e79eeb369cad4cb5578c0a64dcd7c4cf
  Stored in directory: /home/travis/.cache/pip/wheels/07/2f/1d/00dd98e4de351e52b04ad341a254fe6bebf7c6edb9523ad9a1
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.5