pypa/pip · error · ClassNotFound

no lexer for filename {_fn!r} found

Error message

no lexer for filename {_fn!r} found

What it means

Raised by get_lexer_for_filename when no lexer's filename glob pattern (builtin or plugin) matches the basename of the supplied filename. If multiple match, analyse_text breaks the tie; if none match, this is raised.

Source

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

        return matches[-1][0]


def get_lexer_for_filename(_fn, code=None, **options):
    """Get a lexer for a filename.

    Return a `Lexer` subclass instance that has a filename pattern
    matching `fn`. The lexer is given the `options` at its
    instantiation.

    Raise :exc:`pygments.util.ClassNotFound` if no lexer for that filename
    is found.

    If multiple lexers match the filename pattern, use their ``analyse_text()``
    methods to figure out which one is more appropriate.
    """
    res = find_lexer_class_for_filename(_fn, code)
    if not res:
        raise ClassNotFound(f'no lexer for filename {_fn!r} found')
    return res(**options)


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:

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Use guess_lexer (content-based) or get_lexer_by_name with an explicit alias.
  2. Register a plugin lexer whose filenames glob covers the extension.
  3. Map known custom extensions to aliases yourself before calling pygments.

Example fix

# before
lex = get_lexer_for_filename('notes.xyz')
# after
lex = guess_lexer(text)  # or get_lexer_by_name('text')
Defensive patterns

Strategy: fallback

Validate before calling

from pygments.lexers import get_lexer_for_filename, get_lexer_by_name, TextLexer
from pygments.util import ClassNotFound
def lexer_for(fn, fallback='text'):
    try:
        return get_lexer_for_filename(fn)
    except ClassNotFound:
        return get_lexer_by_name(fallback)

Try / catch

from pygments.util import ClassNotFound
from pygments.lexers import TextLexer
try:
    lex = get_lexer_for_filename(fn)
except ClassNotFound:
    lex = TextLexer()

Prevention

When it happens

Trigger: get_lexer_for_filename('data.xyz') where .xyz isn't registered, or a file with no extension.

Common situations: Highlighting source for an exotic/custom extension; extension only covered by an uninstalled plugin; content-based detection needed instead.

Related errors


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