Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 3 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

Pygments

Colored code for pages, docs and terminals.

Markdown, editors and contentPYPI

GitHubWebsite

1. License

“Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met”

Keep the copyright notice and license text with the copies you ship.

github.com/pygments/pygments/blob/master/LICENSE

From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error

What Pygments does

Pygments is a free Python library that colors program code the way code editors do. Your Python app or docs site gives it a code snippet and the language, and gets back colored HTML, terminal text or other formats. It knows about 600 languages and file formats.

What people build with it

Code samples on a blog or docs site

A site built in Python shows code blocks in articles with colors and line numbers instead of plain gray text.

Colored output in a command-line tool

A Python script that prints code, logs or config files shows them in color in the terminal.

Code examples in project documentation

Documentation built with Sphinx or Material for MkDocs colors its code examples with Pygments.

Code in printed reports and papers

A report generator puts colored code into LaTeX or RTF files for PDFs and word processors, not only web pages.

Code in answers from an AI chat Idea

A Python chat app colors the code blocks in model answers on the server, with a time limit for each snippet.

Code pictures for social posts Idea

Turn a short snippet into a PNG to post where code blocks are not supported; this needs the Pillow library too.

Pygments popularity

71 / 100
Gid Score, #337 overall
287M
downloads a week, pypi
2,226
GitHub stars
61
agent projects use it
Pygments: downloads per weekPyPI, pygments 285,839,687
Pygments: GitHub starspygments/pygments 2,226

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How Pygments is used

  • Pygments is a dependency in 61 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (3.8% of the 1,623 Python ones), #11 of 149 in markdown, editors and content.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,109; those are left out of every share on this page.
  • Projects that use it most often also use Pydantic (78.7%), Click (77.0%), Requests (73.8%).

By coding agent

38 projects, 1.3%
34 projects, 1.0%
19 projects, 0.8%
5 projects, 1.1%

Often used with

In 78.7% of its projects
In 77.0% of its projects
In 73.8% of its projects
In 72.1% of its projects
In 72.1% of its projects

More about Pygments

More in Markdown, editors and content

Alternatives

Pygments This page

BSD-2-ClauseGid 71287M/wk

Shiki

Pick Shiki for JavaScript sites that want code colored with the same grammars and themes as VS Code.

highlight.js

Pick highlight.js to color code in the browser, with automatic language detection.

PrismJS

Pick PrismJS for a small in-browser highlighter with plugins for line numbers and copy buttons.

Compare Pygments with another tool

Get started

Tell your AI assistant

“Use the Python library Pygments (pin Pygments==2.21.0, Python 3.9+) to turn code blocks into highlighted HTML with highlight(code, get_lexer_by_name(lang), HtmlFormatter()), fall back to TextLexer for unknown languages, and add the CSS from HtmlFormatter().get_style_defs('.highlight').”

  1. Install Pygments into your project's Python environment; it needs nothing else.

    pip install Pygments

    From pygments.org/faq/

  2. Pick a lexer for the language and a formatter for the output, then call highlight().

    from pygments import highlight
    from pygments.lexers import PythonLexer
    from pygments.formatters import HtmlFormatter
    
    code = 'print "Hello World"'
    print(highlight(code, PythonLexer(), HtmlFormatter()))
    

    From pygments.org/docs/quickstart/

  3. Make the stylesheet once and add it to your page, or the HTML stays uncolored.

    print(HtmlFormatter().get_style_defs('.highlight'))

    From pygments.org/docs/quickstart/

Mistakes to avoid

Inserting highlight(...) output into a page without any stylesheet
Add HtmlFormatter(style=...).get_style_defs('.highlight') to your CSS, or use HtmlFormatter(noclasses=True) for HTML email

By default the HTML only carries short class names like k and s; the colors live in the stylesheet.

lexer = guess_lexer(snippet) for short chat or markdown code blocks
Use the language tag from the code fence with get_lexer_by_name(), and fall back to TextLexer on ClassNotFound

Guessing scores every lexer on the text; in our run a four-line SQL query was guessed as scdoc.

Highlighting code that visitors paste, inside the web request, with no limit
Cap the input size, run highlighting in a separate process with a timeout, and limit how many run at once

Some inputs make Pygments run for a very long time or use a lot of memory, which can take the server down.

Using ImageFormatter (PNG or JPG output) with only Pygments installed
Install Pillow as well before using the image formatters

The image formatters draw with the Python Imaging Library, which Pygments does not install.

Questions

Why is my highlighted HTML still black and white?

The HTML uses class names; you also need the stylesheet from get_style_defs(), or noclasses=True for inline colors.

Can it figure out the language by itself?

It can guess from a file name or the text, but guessing short snippets is unreliable; pass the language when you know it.

Can I use it from the command line?

Yes. The pygmentize command colors a file in the terminal or writes an HTML file.

My security scanner flags a Pygments advisory. Am I at risk?

The project says the risk is slow processing of untrusted input in a service; a docs build that does not take user input is not exposed.

Which Python version does it need?

Pygments 2.21.0 needs Python 3.9 or newer.