Skip to content

util: fix pkg_resources replacement on python3.11

Jérome Perrin requested to merge fix/pkg_resources into master

On python3.11 we cannot yet use importlib.resources, because of errors like this:

$ uv run --python=3.11 --with-editable=. --with='setuptools<70' python -c 'from slapos.grid import slapgrid'
warning: No `requires-python` value found in the workspace. Defaulting to `>=3.11`.
Traceback (most recent call last):
   File "<string>", line 1, in <module>
   File "/home/jerome/src/slapos.core/slapos/grid/slapgrid.py", line 73, in <module>
      from slapos.grid.SlapObject import Software, Partition
   File "/home/jerome/src/slapos.core/slapos/grid/SlapObject.py", line 70, in <module>
      get_package_resource_bytes(
   File "/home/jerome/src/slapos.core/slapos/util.py", line 74, in get_package_resource_bytes
      return importlib.resources.files(package_name).joinpath(resource_filename).read_bytes()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/jerome/.local/share/uv/python/cpython-3.11.11-linux-x86_64-gnu/lib/python3.11/importlib/resources/_common.py", line 22, in files
      return from_package(get_package(package))
                           ^^^^^^^^^^^^^^^^^^^^
   File "/home/jerome/.local/share/uv/python/cpython-3.11.11-linux-x86_64-gnu/lib/python3.11/importlib/resources/_common.py", line 55, in get_package
      raise TypeError(f'{package!r} is not a package')
   TypeError: 'slapos.grid.SlapObject' is not a package
Edited by Jérome Perrin

Merge request reports