如何修复Python 3.10上的importlib,以便它可以正确调用entry_points()?

huangapple go评论61阅读模式
英文:

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.

英文:

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.

答案1

得分: 1

尝试这个:

>>> 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)


它将列出出现问题的分发路径。重新安装或删除它们将停止错误。

[这个人遇到了相同的问题](https://github.com/pypa/twine/issues/774#issuecomment-886657451)
英文:

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

huangapple
  • 本文由 发表于 2023年4月4日 11:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定