pypa/pip · error · ClassNotFound

no lexer for mimetype {_mime!r} found

Error message

no lexer for mimetype {_mime!r} found

What it means

Raised by get_lexer_for_mimetype when no lexer's mimetype list (builtin or plugin) contains the supplied mimetype string.

Source

Thrown at src/pip/_vendor/pygments/lexers/__init__.py:247


def get_lexer_for_mimetype(_mime, **options):
    """
    Return a `Lexer` subclass instance that has `mime` in its mimetype
    list. The lexer is given the `options` at its instantiation.

    Will raise :exc:`pygments.util.ClassNotFound` if not lexer for that mimetype
    is found.
    """
    for modname, name, _, _, mimetypes in LEXERS.values():
        if _mime in mimetypes:
            if name not in _lexer_cache:
                _load_lexers(modname)
            return _lexer_cache[name](**options)
    for cls in find_plugin_lexers():
        if _mime in cls.mimetypes:
            return cls(**options)
    raise ClassNotFound(f'no lexer for mimetype {_mime!r} found')


def _iter_lexerclasses(plugins=True):
    """Return an iterator over all lexer classes."""
    for key in sorted(LEXERS):
        module_name, name = LEXERS[key][:2]
        if name not in _lexer_cache:
            _load_lexers(module_name)
        yield _lexer_cache[name]
    if plugins:
        yield from find_plugin_lexers()


def guess_lexer_for_filename(_fn, _text, **options):
    """
    As :func:`guess_lexer()`, but only lexers which have a pattern in `filenames`
    or `alias_filenames` that matches `filename` are taken into consideration.

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Map the mimetype to a known lexer alias yourself and use get_lexer_by_name.
  2. Register a plugin lexer that advertises the mimetype.
  3. Verify the mimetype against get_all_lexers() mimetypes.

Example fix

# before
lex = get_lexer_for_mimetype('application/x-foo')
# after
lex = get_lexer_by_name('python')  # explicit mapping
Defensive patterns

Strategy: fallback

Validate before calling

MIME_FALLBACK = {'application/x-foo': 'python'}
from pygments.lexers import get_lexer_for_mimetype, get_lexer_by_name
from pygments.util import ClassNotFound
def lexer_for_mime(mime):
    try:
        return get_lexer_for_mimetype(mime)
    except ClassNotFound:
        return get_lexer_by_name(MIME_FALLBACK.get(mime, 'text'))

Try / catch

from pygments.util import ClassNotFound
from pygments.lexers import TextLexer
try:
    lex = get_lexer_for_mimetype(mime)
except ClassNotFound:
    lex = TextLexer()

Prevention

When it happens

Trigger: get_lexer_for_mimetype('application/x-foo') where no lexer advertises that mimetype, or a typo'd mimetype.

Common situations: Web app dispatching highlight off a Content-Type header for an unsupported type; non-standard mimetype; plugin providing the mimetype not installed.

Related errors


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