Python is often being built or distributed without its full standard library. It would be nice if the error messages hinted at why a particular module is missing, and perhaps what to do about it.
Myself and @encukou propose a mechanism to improve error messages by default, and for allowing distributors to provide more informative error messages to users when removed standard library
modules fail to import. (Note that this supersedes the remaining parts of PEP 534 that didn’t already get implemented since the PEP was proposed.)
For example, if CPython is built without the third-party zlib library:
>>> import zlib Traceback (most recent call last): File "<python-input-0>", line 1, in <module> import zlib ModuleNotFoundError: No module named 'zlib' An example from Fedora 42 and Python 3.14, when the python3.14-tkinter
package is not installed:
>>> import tkinter Traceback (most recent call last): File "<python-input-0>", line 1, in <module> import tkinter ModuleNotFoundError: No module named 'tkinter' Proposed changes
At the end of the build, the check_extension_modules.py script will compile information about why modules are missing to a _stdlib_module_info module. This will then be used in the traceback module for nicer error messages.
To allow distributors to add platform-specific information, let’s add a new configure option, --with-missing-stdlib-config=FILE. This will take the location of a JSON file that maps module names
to error messages. For example:
{ "_tkinter": "Install the python-tkinter package to use tkinter", } This would provide a cleaner solution than the patching of specific modules (e.g. Debian patch) or
sys.excepthook as is currently done by several distributors.
A draft implementation can be found on here.
What do you think?