{"id":"57d3844728773dd3","repo":"pytest-dev/pytest","slug":"pytest-theme-environment-variable-has-an-invalid-v","errorCode":null,"errorMessage":"PYTEST_THEME environment variable has an invalid value: '{theme}'. Hint: See available pygments styles with `pygmentize -L styles`.","messagePattern":"PYTEST_THEME environment variable has an invalid value: '(.+?)'\\. Hint: See available pygments styles with `pygmentize -L styles`\\.","errorType":"validation","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/_io/terminalwriter.py","lineNumber":225,"sourceCode":"\n    def _get_pygments_lexer(self, lexer: Literal[\"python\", \"diff\"]) -> Lexer:\n        if lexer == \"python\":\n            return PythonLexer()\n        elif lexer == \"diff\":\n            return DiffLexer()\n        else:\n            assert_never(lexer)\n\n    def _get_pygments_formatter(self) -> TerminalFormatter:\n        from _pytest.config.exceptions import UsageError\n\n        theme = os.getenv(\"PYTEST_THEME\")\n        theme_mode = os.getenv(\"PYTEST_THEME_MODE\", \"dark\")\n\n        try:\n            return TerminalFormatter(bg=theme_mode, style=theme)\n        except pygments.util.ClassNotFound as e:\n            raise UsageError(\n                f\"PYTEST_THEME environment variable has an invalid value: '{theme}'. \"\n                \"Hint: See available pygments styles with `pygmentize -L styles`.\"\n            ) from e\n        except pygments.util.OptionError as e:\n            raise UsageError(\n                f\"PYTEST_THEME_MODE environment variable has an invalid value: '{theme_mode}'. \"\n                \"The allowed values are 'dark' (default) and 'light'.\"\n            ) from e\n\n    def _highlight(\n        self, source: str, lexer: Literal[\"diff\", \"python\"] = \"python\"\n    ) -> str:\n        \"\"\"Highlight the given source if we have markup support.\"\"\"\n        if not source or not self.hasmarkup or not self.code_highlight:\n            return source\n\n        pygments_lexer = self._get_pygments_lexer(lexer)\n        pygments_formatter = self._get_pygments_formatter()","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/_io/terminalwriter.py#L207-L243","documentation":"Pytest highlights code in tracebacks using the pygments library. The style is controlled by the PYTEST_THEME environment variable. If the value does not match any registered pygments style name, pygments raises ClassNotFound, which pytest wraps as a UsageError at config time. This prevents the terminal formatter from being constructed with a nonexistent style.","triggerScenarios":"Setting PYTEST_THEME to a typo or invented name (e.g., PYTEST_THEME=monokaix) before running pytest. The error fires inside _get_pygments_formatter() when TerminalFormatter(bg=theme_mode, style=theme) is constructed and pygments cannot resolve the style name.","commonSituations":"CI pipelines or shell profiles that export PYTEST_THEME with a value copied from an outdated blog post, a value valid on a different pygments version, or a simple typo. Also occurs when the style is removed or renamed across pygments upgrades.","solutions":["Run `pygmentize -L styles` to list valid style names, then set PYTEST_THEME to one of them.","Unset PYTEST_THEME entirely to fall back to the pygments default style.","Check for typos and case sensitivity in the current PYTEST_THEME value."],"exampleFix":"# before\nexport PYTEST_THEME=monokaix  # typo\n\n# after\nexport PYTEST_THEME=monokai\n# verify with: pygmentize -L styles","handlingStrategy":"validation","validationCode":"import os\nfrom pygments.styles import get_style_by_name\n\ntheme = os.getenv('PYTEST_THEME')\nif theme:\n    try:\n        get_style_by_name(theme)\n    except Exception:\n        raise ValueError(f\"PYTEST_THEME='{theme}' is not a valid pygments style. Run: pygmentize -L styles\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate PYTEST_THEME against pygmentize -L styles output in CI setup scripts.","Document the valid theme names in your project README or contributor guide.","Run `pygmentize -L styles` after upgrading pygments to confirm theme names haven't changed."],"tags":["config","environment-variable","pygments","terminal"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}