pypa/pip · error · ClassNotFound

Could not find style class {cls!r} in style module.

Error message

Could not find style class {cls!r} in style module.

What it means

Pygments raises ClassNotFound (a ValueError subclass) from get_style_by_name when the style module imports successfully but does not expose the expected style class attribute. The expected class name is derived by title-casing the requested style name and appending 'Style' (e.g. 'monokai' -> 'MonokaiStyle'), so a typo, a renamed class, or a custom module missing that attribute triggers it.

Source

Thrown at src/pip/_vendor/pygments/styles/__init__.py:53

    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. Verify the style name against pygments.styles.get_all_styles() and correct the typo.
  2. If using a custom style, ensure its class is named <Name.title()>Style inside a module under pygments.styles.
  3. Register the style via a setuptools entry point in the 'pygments.styles' group so find_plugin_styles() can locate it.

Example fix

// before
formatter = HtmlFormatter(style='monokia')

// after
formatter = HtmlFormatter(style='monokai')
Defensive patterns

Strategy: validation

Validate before calling

from pip._vendor.pygments.styles import get_all_styles
valid = set(get_all_styles())
name = 'monokai'
if name not in valid:
    raise ValueError(f'unknown style {name!r}; choose from {sorted(valid)}')
formatter = HtmlFormatter(style=name)

Try / catch

from pip._vendor.pygments.util import ClassNotFound
try:
    style = get_style_by_name(name)
except ClassNotFound:
    style = get_style_by_name('default')  # fallback style

Prevention

When it happens

Trigger: Calling pygments.styles.get_style_by_name(name) with a name that is neither in the built-in STYLE_MAP nor a plugin style, where the fallback import of 'pygments.styles.<name>' succeeds but the module lacks an attribute named '<Name.title()>Style'. Also triggered by HtmlFormatter(style='nonexistent') or any formatter/lexer that resolves a style by name.

Common situations: Typos in a style name passed to a formatter (e.g. 'monokia' instead of 'monokai'); third-party style plugins that define a class whose name does not match the '<Name>Style' convention; installing a custom style file into the package without the correctly-named class.

Related errors


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