pypa/pip · error · ClassNotFound
Could not find style module {mod!r}
Error message
Could not find style module {mod!r} What it means
Raised by get_style_by_name when __import__ of the resolved style module raises ImportError. For builtin names the message adds 'though it should be builtin', indicating a corrupt/incomplete pygments install; for custom names it means no such style module exists.
Source
Thrown at src/pip/_vendor/pygments/styles/__init__.py:47
Will raise :exc:`pygments.util.ClassNotFound` if no style of that name is
found.
"""
if name in _STYLE_NAME_TO_MODULE_MAP:
mod, cls = _STYLE_NAME_TO_MODULE_MAP[name]
builtin = "yes"
else:
for found_name, style in find_plugin_styles():
if name == found_name:
return style
# perhaps it got dropped into our styles package
builtin = ""
mod = 'pygments.styles.' + name
cls = name.title() + "Style"
try:
mod = __import__(mod, None, None, [cls])
except ImportError:
raise ClassNotFound(f"Could not find style module {mod!r}" +
(builtin and ", though it should be builtin")
+ ".")
try:
return getattr(mod, cls)
except AttributeError:
raise ClassNotFound(f"Could not find style class {cls!r} in style module.")
def get_all_styles():
"""Return a generator for all styles by name, both builtin and plugin."""
for v in STYLES.values():
yield v[1]
for name, _ in find_plugin_styles():
yield name
View on GitHub (pinned to d7d0d0a394)
Solutions
- List valid names via get_all_styles() and use an exact one.
- For a missing builtin, reinstall pygments cleanly.
- For a custom style, ensure its module is importable and on sys.path.
- Fix the typo.
Example fix
# before
style = get_style_by_name('monokaii') # typo
# after
style = get_style_by_name('monokai') Defensive patterns
Strategy: try-catch
Validate before calling
from pygments.styles import get_all_styles
KNOWN = set(get_all_styles())
if name not in KNOWN:
name = 'default' Try / catch
from pygments.util import ClassNotFound
from pygments.styles import get_style_by_name, get_style_by_name
try:
style = get_style_by_name(name)
except ClassNotFound:
style = get_style_by_name('default') Prevention
- Validate style names against get_all_styles().
- Reinstall pygments if a builtin style module is missing.
When it happens
Trigger: get_style_by_name('nonexistent'), a typo'd style name, or a builtin style whose submodule is missing from a broken pygments installation.
Common situations: Typo in the style name from config/CLI; custom style module not on the path; partial/corrupt pygments package missing a styles submodule; style removed in a newer pygments version.
Related errors
- filter {filtername!r} not found
- no formatter found for name {_alias!r}
- no formatter found for file name {fn!r}
- no lexer for alias {_alias!r} found
- no lexer for filename {_fn!r} found
AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04).
Data as JSON: /data/errors/75ff532f7fe3fd0c.json.
Report an issue: GitHub.