Skip to content

runpy.run_module for a non-existent module raises an ImportError without name set to None #149117

@godlygeek

Description

@godlygeek

Bug report

Bug description:

When import fails to import a module, it raises an ImportError with the name attribute set to the module it failed to import.

When runpy.run_module fails to import a module, it raises an ImportError with the name attribute set to None.

This seems to be an accidental difference, or at least an undocumented one:

>>> try:
...     import foobar
... except ImportError as e:
...     print(f"{e=} {e.name=}")
...
e=ModuleNotFoundError("No module named 'foobar'") e.name='foobar'

>>> try:
...     import runpy
...     runpy.run_module("foobar")
... except ImportError as e:
...     print(f"{e=} {e.name=}")
...
e=ImportError('No module named foobar') e.name=None

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions