pypa/pip · warning · ClassNotFound
no lexer matching the text found
Error message
no lexer matching the text found
What it means
Raised by guess_lexer() as a ClassNotFound when, after iterating every lexer class via _iter_lexerclasses() and calling analyse_text(), the best score is 0.0 or no lexer was assigned (line 338-339). No lexer's content heuristics returned a positive value and no vim modeline resolved either.
Solutions
- Provide more representative sample text — analyse_text heuristics need enough content to exceed the 0.0 floor.
- Fall back to a default lexer such as get_lexer_by_name('text') when detection fails.
- If you know the language, call get_lexer_by_name() directly instead of guessing.
Example fix
// before
lexer = guess_lexer(short_or_empty_text)
// after
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.util import ClassNotFound
try:
lexer = guess_lexer(text)
except ClassNotFound:
lexer = get_lexer_by_name('text') Defensive patterns
Strategy: fallback
Validate before calling
from pygments.lexers import _iter_lexerclasses
def best_score(text):
scores = [(lx.analyse_text(text), lx) for lx in _iter_lexerclasses()]
scores.sort(key=lambda t: t[0])
return scores[-1]
def is_detectable(text):
return best_score(text)[0] > 0.0 Try / catch
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.util import ClassNotFound
try:
lexer = guess_lexer(text)
except ClassNotFound:
lexer = get_lexer_by_name('text') Prevention
- Supply a sufficiently large, representative text sample to analyse_text heuristics.
- Default to a known lexer (e.g. 'text') when content detection fails rather than letting it raise.
When it happens
Trigger: Calling guess_lexer() on very short, ambiguous, or binary-decoded text that no lexer's analyse_text() can positively identify; text that is valid but extremely generic (plain ASCII with no distinguishing tokens).
Common situations: Auto-detecting a language for a one-line file or a file full of whitespace/numbers; feeding text that decodes ambiguously via guess_decode(); a buffer where every analyse_text returns 0.0 by default.
Related errors
AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08).
Data as JSON: /api/errors/7db72898fdaca455.
Report an issue: GitHub.
Appendix: source
Thrown at src/pip/_vendor/pygments/lexers/__init__.py:339
# try to get a vim modeline first
ft = get_filetype_from_buffer(_text)
if ft is not None:
try:
return get_lexer_by_name(ft, **options)
except ClassNotFound:
pass
best_lexer = [0.0, None]
for lexer in _iter_lexerclasses():
rv = lexer.analyse_text(_text)
if rv == 1.0:
return lexer(**options)
if rv > best_lexer[0]:
best_lexer[:] = (rv, lexer)
if not best_lexer[0] or best_lexer[1] is None:
raise ClassNotFound('no lexer matching the text found')
return best_lexer[1](**options)
class _automodule(types.ModuleType):
"""Automatically import lexers."""
def __getattr__(self, name):
info = LEXERS.get(name)
if info:
_load_lexers(info[0])
cls = _lexer_cache[info[1]]
setattr(self, name, cls)
return cls
if name in COMPAT:
return getattr(self, COMPAT[name])
raise AttributeError(name)
View on GitHub (pinned to f399c37189)