{"id":269,"library":"Pygments","title":"Pygments","description":"Pygments is a generic syntax highlighting library written in Python, supporting over 500 languages and text formats with output in HTML, LaTeX, RTF, SVG, image formats, and ANSI terminal sequences. It can be used both as a library and as the `pygmentize` CLI tool. Current version is 2.19.2. Releases are made periodically with new lexers added each minor version; patch releases fix regressions quickly (2.19.1 and 2.19.2 followed 2.19.0 within weeks).","status":"active","version":"2.19.2","language":"python","source_language":"en","source_url":"https://github.com/pygments/pygments","tags":["syntax-highlighting","lexer","formatter","html","terminal","cli","code","documentation"],"install":[{"cmd":"pip install Pygments","lang":"bash","label":"Standard install"},{"cmd":"pip install Pygments[windows-terminal]","lang":"bash","label":"Windows colored console output (Windows only)"}],"dependencies":[],"imports":[{"note":"Top-level entry point for all highlighting; always import from `pygments` directly.","symbol":"highlight","correct":"from pygments import highlight"},{"note":"All lexer classes live in `pygments.lexers`, not in the top-level `pygments` package.","wrong":"from pygments import PythonLexer","symbol":"PythonLexer (and other named lexers)","correct":"from pygments.lexers import PythonLexer"},{"note":"Dynamic lexer lookup by alias string, e.g. 'python'. Raises pygments.util.ClassNotFound on unknown alias.","symbol":"get_lexer_by_name","correct":"from pygments.lexers import get_lexer_by_name"},{"note":"Selects lexer by filename pattern. Only uses the primary (unique) filename list; use guess_lexer_for_filename for ambiguous extensions.","symbol":"get_lexer_for_filename","correct":"from pygments.lexers import get_lexer_for_filename"},{"note":"Analyses text content to pick a lexer. Raises ClassNotFound if no lexer scores above 0. May be slow on large inputs.","symbol":"guess_lexer","correct":"from pygments.lexers import guess_lexer"},{"note":"All formatter classes live in `pygments.formatters`, not the top-level package.","wrong":"from pygments import HtmlFormatter","symbol":"HtmlFormatter","correct":"from pygments.formatters import HtmlFormatter"},{"note":"Exception raised by all lexer/formatter/style lookup functions when no match is found. Must be caught explicitly.","symbol":"ClassNotFound","correct":"from pygments.util import ClassNotFound"},{"note":"Returns an iterator of all built-in and plugin style names. Use instead of STYLE_MAP for complete plugin-aware enumeration.","symbol":"get_all_styles","correct":"from pygments.styles import get_all_styles"},{"note":"Base class for custom lexer development. Note: singular `pygments.lexer`, not `pygments.lexers`.","symbol":"RegexLexer","correct":"from pygments.lexer import RegexLexer"}],"quickstart":{"code":"from pygments import highlight\nfrom pygments.lexers import get_lexer_by_name\nfrom pygments.formatters import HtmlFormatter\nfrom pygments.util import ClassNotFound\n\ncode = '''\ndef greet(name: str) -> str:\n    return f\"Hello, {name}!\"\n\nprint(greet(\"World\"))\n'''\n\ntry:\n    lexer = get_lexer_by_name('python', stripall=True)\nexcept ClassNotFound as e:\n    raise SystemExit(f\"Lexer not found: {e}\")\n\n# cssclass must match the selector passed to get_style_defs\nformatter = HtmlFormatter(linenos=True, cssclass='highlight', style='default')\n\n# highlighted is an HTML snippet — NOT a full document\nhighlighted = highlight(code, lexer, formatter)\n\n# get_style_defs() must be called to obtain the CSS; it is NOT embedded by default\ncss = formatter.get_style_defs('.highlight')\n\nprint(f'<style>\\n{css}\\n</style>')\nprint(highlighted)\n","lang":"python","description":"Highlight a Python snippet to HTML and print the required CSS alongside it."},"warnings":[{"fix":"Call `HtmlFormatter().get_style_defs('.highlight')` and embed or link the result. Use `HtmlFormatter(full=True)` to get a self-contained HTML document, or `noclasses=True` for inline styles (not recommended for large code blocks).","message":"HtmlFormatter output does NOT include CSS by default. The generated HTML uses CSS classes but the stylesheet must be obtained separately via `formatter.get_style_defs('.highlight')` and injected into the page. Without it the output appears unstyled.","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to Python >=3.8. Remove any explicit `importlib-metadata` dependency added for Pygments plugin discovery.","message":"Python 3.7 and below are no longer supported as of Pygments 2.18.0. The `importlib-metadata` backport is no longer required or used. The `pip install Pygments[plugins]` extra is a no-op.","severity":"breaking","affected_versions":"<2.18.0"},{"fix":"Always wrap lookup calls in `try/except pygments.util.ClassNotFound`. Fall back to `get_lexer_by_name('text')` (the plain-text lexer) for safe passthrough.","message":"All lexer/formatter/style lookup functions (`get_lexer_by_name`, `get_lexer_for_filename`, `get_formatter_by_name`, etc.) raise `pygments.util.ClassNotFound` — not a built-in like `KeyError` or `ValueError` — when no match is found. Uncaught, this crashes silently in many frameworks.","severity":"gotcha","affected_versions":"all"},{"fix":"Always enforce a timeout when calling Pygments on untrusted user input (e.g. run in a subprocess with `subprocess.run(..., timeout=5)`). Limit concurrent Pygments processes to avoid resource exhaustion.","message":"Pygments provides no execution-time guarantees. Certain inputs (especially adversarial or malformed code) can trigger catastrophic backtracking in lexer regexes, causing the process to hang or consume excessive memory. This is a known DoS vector for web services.","severity":"gotcha","affected_versions":"all"},{"fix":"For ambiguous extensions use `guess_lexer_for_filename(filename, content)`. Always catch `ClassNotFound` from both functions.","message":"`get_lexer_for_filename()` only checks the primary (unique) filename list and can raise `ClassNotFound` for ambiguous extensions like `.html`. `guess_lexer_for_filename()` also checks secondary patterns and runs content analysis, but is slower.","severity":"gotcha","affected_versions":"all"},{"fix":"Keep them in sync: `fmt = HtmlFormatter(cssclass='source')` then `fmt.get_style_defs('.source')`. Or rely on the default cssclass `'highlight'` and pass `'.highlight'` to `get_style_defs()`.","message":"The `cssclass` option on `HtmlFormatter` must match the selector prefix passed to `get_style_defs()`. If you set `cssclass='source'` but call `get_style_defs('.highlight')`, the CSS will not apply to the generated markup.","severity":"gotcha","affected_versions":"all"},{"fix":"Use `from pygments.styles import get_all_styles; list(get_all_styles())` to enumerate all styles including those registered via plugins.","message":"`STYLE_MAP` in `pygments.styles` uses an older format and does not include plugin styles. It is kept for backwards compatibility but is incomplete.","severity":"deprecated","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T12:39:30.725Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install Pygments using pip: `pip install pygments`.","cause":"The Pygments library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'pygments'"},{"fix":"Ensure Pygments is installed: `pip install pygments`.","cause":"The Pygments library is not installed or not accessible in the current Python environment.","error":"ImportError: No module named pygments.styles"},{"fix":"Ensure Pygments is installed: `pip install pygments`.","cause":"The Pygments library is not installed or not accessible in the current Python environment.","error":"ImportError: No module named 'pygments.lexer'"},{"fix":"Ensure Pygments is installed: `pip install pygments`.","cause":"The Pygments library is not installed or not accessible in the current Python environment.","error":"ImportError: No module named 'pygments.lexers._asy_builtins'"},{"fix":"Ensure Pygments is installed: `pip install pygments`.","cause":"The Pygments library is not installed or not accessible in the current Python environment.","error":"ImportError: No module named 'pygments'"}],"ecosystem":"pypi","meta_description":null,"install_score":80,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"26.3M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"26.5M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"27M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"27M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"29.0M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"29.2M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"29M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"30M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"20.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21.0M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"20.4M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"20.7M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"21M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"25.7M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"26.0M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"26M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"windows-terminal","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"26M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}