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

  1. List valid names via get_all_styles() and use an exact one.
  2. For a missing builtin, reinstall pygments cleanly.
  3. For a custom style, ensure its module is importable and on sys.path.
  4. 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

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


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