Search code examples
pythontypeerrorentry-pointpython-importlib

How can I fix importlib on python3.10 so that it can call entry_points() properly?


I am using Python 3.10 on Ubuntu 22.04 working on a project that uses Farama Foundation's gymnasium library. When gymnasium is imported, it uses importlib to get entry points, but when I ran import gymnasium into IDLE I got the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "<pyshell#2>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/gymnasium/__init__.py", line 12, in <module>
    from gymnasium.envs.registration import (
  File "/usr/local/lib/python3.10/dist-packages/gymnasium/envs/__init__.py", line 382, in <module>
    load_plugin_envs()
  File "/usr/local/lib/python3.10/dist-packages/gymnasium/envs/registration.py", line 565, in load_plugin_envs
    for plugin in metadata.entry_points(group=entry_point):
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 1009, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 459, in load
    ordered = sorted(eps, key=by_group)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 1006, in <genexpr>
    eps = itertools.chain.from_iterable(
  File "/usr/lib/python3.10/importlib/metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 941, in _normalized_name
    return self._name_from_stem(stem) or super()._normalized_name
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 622, in _normalized_name
    return Prepared.normalize(self.name)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 871, in normalize
    return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
  File "/usr/lib/python3.10/re.py", line 209, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

I asked one of the creators of gymnasium about this issue on GitHub, and they asked me to print out a list of entry points, so I ran the following:

from importlib.metadata import *
eps = entry_points()

and got the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "<pyshell#1>", line 1, in <module>
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 1009, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 459, in load
    ordered = sorted(eps, key=by_group)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 1006, in <genexpr>
    eps = itertools.chain.from_iterable(
  File "/usr/lib/python3.10/importlib/metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 941, in _normalized_name
    return self._name_from_stem(stem) or super()._normalized_name
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 622, in _normalized_name
    return Prepared.normalize(self.name)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 871, in normalize
    return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
  File "/usr/lib/python3.10/re.py", line 209, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

What this seems to suggest is that one of the names that entry_points() is searching for is not in the correct format. I am unsure of how to fix this. I am willing to uninstall Python 3.10 completely and reinstall it and all additional packages from scratch, but I would prefer an approach that is less aggressive.


Solution

  • Try this:

    >>> import importlib_metadata as md
    >>> dists = md.distributions()
    >>> broken = [dist for dist in dists if dist.name is None]
    >>> for dist in broken:
    ...     print(dist._path)
    

    It will list the paths of distributions that are the problem. Reinstalling or deleting them will stop the error.

    This guy had the same problem