-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Running importlib.resources.files() without specifying the anchor parameter in interactive REPL in Python 3.12 raises a rather unhelpful error:
>>> import importlib.resources
>>> importlib.resources.files()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.12/importlib/resources/_common.py", line 45, in wrapper
return func()
^^
File "/usr/lib64/python3.12/importlib/resources/_common.py", line 56, in files
return from_package(resolve(anchor))
^^
File "/usr/lib64/python3.12/importlib/resources/_common.py", line 113, in from_package
reader = spec.loader.get_resource_reader(spec.name)
^^
File "/usr/lib64/python3.12/importlib/resources/_adapters.py", line 17, in __getattr__
return getattr(self.spec, name)
^^
AttributeError: 'NoneType' object has no attribute 'name'It would be nice to return something more helpful. The documentation doesn't mention that this function can raise, so I am assuming that raising AttributeError is not part of the public API?
However, on a locally built Python from current main branch, this call returns a path to Lib/_pyrepl. Even more surprising though is that this also happens when running the OLD REPL with TERM=dumb ./python
❯ TERM=dumb ./python
Python 3.14.0a0 (heads/main:1a84bdc237, Jun 30 2024, 21:44:46) [GCC 13.3.1 20240522 (Red Hat 13.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
warning: can't use pyrepl: terminal doesn't have the required clear capability
>>> import importlib.resources
>>> importlib.resources.files()
PosixPath('/home/hollas/software/cpython/Lib/_pyrepl')CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
lysnikolaou commentedon Jul 1, 2024
TERM=dumbdoes not enable the old REPL. The env var is calledPYTHON_BASIC_REPL.danielhollas commentedon Jul 1, 2024
Well, while it may not enable it explicitly, implicitly it does since pyrepl will not start with this type of terminal.
But you are right that actually using
PYTHON_BASIC_REPLwill actually run the old REPL directly, and will produce the same behaviour as in 3.12Note that I am not saying that the pyrepl behaviour is wrong here, just noting the difference. If anything, I'd change the behaviour in the old REPL so that at least it fails a bit more gracefully (not an obscure AttributeError).
whitphx commentedon May 22, 2025
#134275 should fix the new REPL's different behavior than the old one,
while the issue below still is there
whitphx commentedon Sep 5, 2025
Hi from PyConTW sprint.
SpecLoaderAdapteraccessesspec's attributes such asspec.namecpython/Lib/importlib/resources/_adapters.py
Line 17 in fc0305a
while
speccan beNonewhen thepackageis__main__as documented.cpython/Lib/importlib/resources/_adapters.py
Line 168 in fc0305a
importlib.resources._common.from_package(package)needs to take care of the case wherepackage.__spec__isNone.Let me take a closer look...
importlib.resourcessynched with https://github.com/python/importlib_resources, andimportlib_resourcesalso has the same problem as this issue.importlib.resources.files()when module spec is None #138531Merge branch 'main' into pythongh-121190