{"id":519,"library":"ipython-pygments-lexers","title":"IPython Pygments Lexers","description":"The ipython-pygments-lexers library provides specialized Pygments lexers for syntax highlighting IPython code and interactive sessions. These lexers were originally part of the main IPython project but were later extracted into a dedicated package to reduce IPython's core dependencies. The package is actively maintained, with its current version being 1.1.1, and releases occur infrequently as needed for bug fixes or minor enhancements.","status":"active","version":"1.1.1","language":"python","source_language":"en","source_url":"https://github.com/ipython/ipython-pygments-lexers","tags":["IPython","Pygments","lexer","syntax highlighting","console","code"],"install":[{"cmd":"pip install ipython-pygments-lexers","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the core syntax highlighting framework that these lexers extend.","package":"Pygments","optional":false},{"reason":"Requires Python 3.8 or newer to run.","package":"python","optional":false}],"imports":[{"note":"Lexer for pure IPython code (Python + magic/shell commands).","symbol":"IPythonLexer","correct":"from ipython_pygments_lexers import IPythonLexer"},{"note":"Lexer for full IPython console sessions, including prompts, output, and tracebacks.","symbol":"IPythonConsoleLexer","correct":"from ipython_pygments_lexers import IPythonConsoleLexer"},{"note":"This intelligent lexer (recommended for most general uses) determines whether to use IPythonLexer or IPythonConsoleLexer based on the input text. The old import path is from the monolithic IPython package before lexers were split out.","wrong":"from IPython.lib.lexers import IPyLexer","symbol":"IPyLexer","correct":"from ipython_pygments_lexers import IPyLexer"}],"quickstart":{"code":"from pygments import highlight\nfrom pygments.formatters import HtmlFormatter\nfrom ipython_pygments_lexers import IPyLexer\n\ncode_to_highlight = (\n    \"\"\"In [1]: import numpy as np\n   ...: a = np.array([1, 2, 3])\n   ...: print(a * 2)\nOut[1]: [2 4 6]\n\nIn [2]: %timeit [i**2 for i in range(1000)]\n40.7 µs ± 1.15 µs per loop (mean ± Std. dev. of 7 runs, 10000 loops each)\n\"\"\"\n)\n\nlexer = IPyLexer()\nformatter = HtmlFormatter()\n\nhighlighted_code = highlight(code_to_highlight, lexer, formatter)\nprint(highlighted_code)","lang":"python","description":"This quickstart demonstrates how to use the recommended `IPyLexer` from `ipython-pygments-lexers` with Pygments' `highlight` function and an `HtmlFormatter` to highlight a typical IPython console session. The `IPyLexer` intelligently adapts to the content, correctly rendering input prompts, outputs, and magic commands."},"warnings":[{"fix":"Install `ipython-pygments-lexers` and update import statements from `from IPython.lib.lexers import ...` to `from ipython_pygments_lexers import ...`.","message":"The Pygments lexers for IPython were extracted from the main `IPython` package (specifically `IPython.lib.lexers`) into this separate `ipython-pygments-lexers` package. Direct imports from `IPython.lib.lexers` will fail if IPython 9.0 or newer is used without this package being installed.","severity":"breaking","affected_versions":"IPython >= 9.0"},{"fix":"Prefer directly importing `IPyLexer` and instantiating it, or understand that the 'ipython' alias refers to the combined console lexer functionality rather than just pure code. For pure input code, `IPythonLexer` can be used.","message":"When using Pygments' `get_lexer_by_name()` or similar lookup functions, the aliases 'ipython' and 'ipython3' may be confusing. Historically, these referred to the `IPyLexer` or `IPythonConsoleLexer`, which handle full console sessions. However, the alias 'ipython' can be misleading as an IPython console session (with prompts/outputs) is distinct from pure IPython code.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T14:36:22.373Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure `ipython-pygments-lexers` is installed. If the issue persists, consider pinning IPython to a version known to be compatible (e.g., `ipython!=8.7.0`). If using Sphinx, ensure `ipython_pygments_lexers` (or `IPython.sphinxext.ipython_console_highlighting` for older setups) is added to the `extensions` list in your `conf.py` file.","cause":"This warning typically arises when the Pygments syntax highlighter cannot find the lexer named 'ipython3', often due to an outdated or incompatible version of IPython, or if the `ipython-pygments-lexers` package is not correctly registering its lexers with Pygments.","error":"WARNING: Pygments lexer name 'ipython3' is not known"},{"fix":"Install the package using pip: `pip install ipython-pygments-lexers`","cause":"The `ipython-pygments-lexers` package has not been installed in the Python environment currently in use.","error":"ModuleNotFoundError: No module named 'ipython_pygments_lexers'"},{"fix":"Install the `Pygments` library: `pip install Pygments`","cause":"The core `Pygments` library, which `ipython-pygments-lexers` depends on for its functionality, is not installed or is not accessible in the active Python environment.","error":"ImportError: No module named pygments"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"cli_name":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.25,"mem_mb":8.1,"disk_size":"26.3M"},{"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.14,"mem_mb":8.1,"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.29,"mem_mb":8.6,"disk_size":"29.0M"},{"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.24,"mem_mb":8.6,"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.31,"mem_mb":9.1,"disk_size":"20.8M"},{"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.33,"mem_mb":9.1,"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.17,"mem_mb":7.9,"disk_size":"20.5M"},{"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.21,"mem_mb":7.9,"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.1,"mem_mb":6.8,"disk_size":"25.8M"},{"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.14,"mem_mb":6.8,"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}]}}