pypa/pip · error · ClassNotFound
no lexer for filename
Error message
no lexer for filename {_fn!r} found What it means
Raised by get_lexer_for_filename() as a ClassNotFound when find_lexer_class_for_filename(_fn, code) returns None (line 225-227), meaning no builtin lexer and no plugin lexer has a filename glob matching basename(_fn). It is the strict counterpart that throws instead of returning None.
Solutions
- Call find_lexer_class_for_filename(fn) first — it returns None instead of raising, so you can branch on a miss.
- Map the extension to a known lexer alias manually and use get_lexer_by_name() instead (e.g., get_lexer_by_name('text') or 'ini').
- Install or register a Pygments plugin / entry point that declares a filename glob covering your extension.
Example fix
// before
lexer = get_lexer_for_filename('config.xyz')
// after
from pygments.lexers import find_lexer_class_for_filename, get_lexer_by_name
lexer_cls = find_lexer_class_for_filename('config.xyz')
lexer = (lexer_cls or get_lexer_by_name('ini'))() Defensive patterns
Strategy: fallback
Validate before calling
from pygments.lexers import find_lexer_class_for_filename
def has_filename_lexer(fn):
return find_lexer_class_for_filename(fn) is not None Try / catch
from pygments.lexers import find_lexer_class_for_filename, get_lexer_by_name
from pygments.util import ClassNotFound
cls = find_lexer_class_for_filename(fn)
lexer = cls() if cls else get_lexer_by_name('text') Prevention
- Prefer the non-raising find_lexer_class_for_filename() and branch on None.
- Maintain an extension-to-alias fallback map for unsupported file types.
When it happens
Trigger: Calling get_lexer_for_filename('file.unknownext') where '.unknownext' matches no registered filename pattern in LEXERS or any plugin; passing a filename whose basename matches no glob in any lexer's filenames tuple.
Common situations: An unsupported or uncommon file extension (e.g., a proprietary config format); a custom file type with no registered lexer; the plugins providing the lexer are not installed in the environment; case-sensitivity issues where the pattern expects lowercase.
Related errors
- no lexer for filename
- no lexer for mimetype
- no lexer matching the text found
- no valid class found in
- cannot read
AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08).
Data as JSON: /api/errors/f9ef33be42480b75.
Report an issue: GitHub.
Appendix: 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 f399c37189)