pypa/pip · error · ClassNotFound

no formatter found for file name {fn!r}

Error message

no formatter found for file name {fn!r}

What it means

Raised by get_formatter_for_filename when no formatter's filename glob pattern (builtin or plugin) matches the basename of the supplied filename. The match uses fnmatch-style globs from each formatter's filenames attribute.

Source

Thrown at src/pip/_vendor/pygments/formatters/__init__.py:137

    """
    Return a :class:`.Formatter` subclass instance that has a filename pattern
    matching `fn`. The formatter is given the `options` at its instantiation.

    Will raise :exc:`pygments.util.ClassNotFound` if no formatter for that filename
    is found.
    """
    fn = basename(fn)
    for modname, name, _, filenames, _ in FORMATTERS.values():
        for filename in filenames:
            if _fn_matches(fn, filename):
                if name not in _formatter_cache:
                    _load_formatters(modname)
                return _formatter_cache[name](**options)
    for _name, cls in find_plugin_formatters():
        for filename in cls.filenames:
            if _fn_matches(fn, filename):
                return cls(**options)
    raise ClassNotFound(f"no formatter found for file name {fn!r}")


class _automodule(types.ModuleType):
    """Automatically import formatters."""

    def __getattr__(self, name):
        info = FORMATTERS.get(name)
        if info:
            _load_formatters(info[0])
            cls = _formatter_cache[info[1]]
            setattr(self, name, cls)
            return cls
        raise AttributeError(name)


oldmod = sys.modules[__name__]
newmod = _automodule(__name__)
newmod.__dict__.update(oldmod.__dict__)

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Call get_formatter_by_name with an explicit formatter alias instead.
  2. Register a plugin formatter whose filenames glob covers the extension.
  3. List supported patterns via get_all_formatters() and each formatter's filenames.

Example fix

# before
fmt = get_formatter_for_filename('notes.xyz')
# after
fmt = get_formatter_by_name('html')
Defensive patterns

Strategy: fallback

Validate before calling

from pygments.formatters import get_formatter_for_filename, get_formatter_by_name
from pygments.util import ClassNotFound
def formatter_for(fn, fallback='html'):
    try:
        return get_formatter_for_filename(fn)
    except ClassNotFound:
        return get_formatter_by_name(fallback)

Try / catch

from pygments.util import ClassNotFound
try:
    fmt = get_formatter_for_filename(fn)
except ClassNotFound:
    fmt = get_formatter_by_name('html')

Prevention

When it happens

Trigger: get_formatter_for_filename('report.xyz') where .xyz isn't a registered extension, or a filename with no extension.

Common situations: Highlighting output for an unusual/custom file type; a filename whose extension isn't mapped to any output formatter; extension handled only by a plugin that isn't installed.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/6a4ff78466388e3a.json. Report an issue: GitHub.