Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

importlib_metadata handles PathLike objects on sys.path when importlib.PathFinder does not #372

Closed
@graingert

Description

@graingert

demo script:

importcontextlibimportosimportpathlibimportsysimporttempfileimportimportlib_metadata@contextlib.contextmanagerdef_tmp_path(*args,**kwargs):withtempfile.TemporaryDirectory(*args,**kwargs)astmp_dir:yieldpathlib.Path(tmp_dir)defmain():with_tmp_path()astmp_path:        (tmp_path/"module.py").write_bytes(b"def function():\n    return 1\n")dist_info=tmp_path/"demo_package-0.0.0.dist-info"dist_info.mkdir()        (dist_info/"entry_points.txt").write_bytes(b"[group]\nname = module:function\n"        )sys.path.append(tmp_path)ep=next(iter(importlib_metadata.entry_points(name="name",group="group")),None        )print(ep)print(ep.load())if__name__=="__main__":sys.exit(main())

results in:

EntryPoint(name='name', value='module:function', group='group')Traceback (most recent call last):  File "/home/graingert/projects/demo_importlib.py", line 33, in <module>    sys.exit(main())             ^^^^^^  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__    self.gen.throw(typ, value, traceback)    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/home/graingert/projects/demo_importlib.py", line 13, in _tmp_path    yield pathlib.Path(tmp_dir)    ^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/home/graingert/projects/demo_importlib.py", line 29, in main    print(ep.load())          ^^^^^^^^^  File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/importlib_metadata/__init__.py", line 203, in load    module = import_module(match.group('module'))             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module    return _bootstrap._gcd_import(name[level:], package, level)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlockedModuleNotFoundError: No module named 'module'

however if Ios.fsdecode thetmp_path - before appending it to thesys.path I get:

EntryPoint(name='name', value='module:function', group='group')<function function at 0x7f2b12192700>

see alsohttps://github.com/python/cpython/blob/23dcea5de736b367c0244042aaca10971538b2b4/Lib/importlib/_bootstrap_external.py#L1460-L1461
andhttps://bugs.python.org/issue32642

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp