{"record":{"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":"exception","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/_io/terminalwriter.py#L207-L243","documentation":"Raised by TerminalWriter._get_pygments_formatter() when the PYTEST_THEME environment variable names a pygments style that does not exist, causing pygments.util.ClassNotFound. pytest wraps this as a UsageError so the user gets an actionable hint listing how to discover valid style names. This fires at code-highlighting time (e.g. when displaying source in tracebacks).","triggerScenarios":"Setting PYTEST_THEME=monokai_old (typo) or PYTEST_THEME=custom_style that isn't installed; running pytest in an environment where the referenced pygments style plugin was removed.","commonSituations":"Dotfile or CI config with a stale theme name; upgrading pygments and a style was renamed/removed; copying a theme setting from another tool that uses different style names.","solutions":["Run `pygmentize -L styles` to list available styles and pick a valid one.","Unset PYTEST_THEME to use the default.","Install the pygments style package that provides the missing style.","Check for typos in the environment variable value."],"exampleFix":"# before\nexport PYTEST_THEME=monokai_old\n# after\nexport PYTEST_THEME=monokai","handlingStrategy":"validation","validationCode":"import os, pygments.styles\ntheme = os.getenv('PYTEST_THEME')\nif theme and theme not in pygments.styles.get_all_styles():\n    del os.environ['PYTEST_THEME']","typeGuard":"def is_valid_theme(name: str) -> bool:\n    import pygments.styles\n    return name in set(pygments.styles.get_all_styles())","tryCatchPattern":null,"preventionTips":["Run `pygmentize -L styles` when configuring PYTEST_THEME.","Unset the variable in CI if unsure."],"tags":["terminalwriter","pygments","environment","config"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}