{"record":{"id":"f3d2d517b7b4e4be","repo":"pytest-dev/pytest","slug":"path-cannot-use-both-pytest-and-tool-pytest","errorCode":null,"errorMessage":"{path}: Cannot use both [pytest] and [tool.pytest]/[tool.pytest.ini_options] in the same file. Please use [pytest], which is what pytest's own configuration files use; the [tool.pytest] tables are meant for pyproject.toml.","messagePattern":"(.+?): Cannot use both \\[pytest\\] and \\[tool\\.pytest\\]/\\[tool\\.pytest\\.ini_options\\] in the same file\\. Please use \\[pytest\\], which is what pytest's own configuration files use; the \\[tool\\.pytest\\] tables are meant for pyproject\\.toml\\.","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/findpaths.py","lineNumber":221,"sourceCode":"\n\ndef _load_custom_toml(path: Path) -> ConfigDict | None:\n    \"\"\"Load a TOML file with an arbitrary name, as passed via ``-c``.\n\n    Such a file reads its configuration from ``[pytest]``, like ``pytest.toml``\n    does -- the table pytest documents for its own files (#14705). The\n    ``pyproject.toml`` tables ``[tool.pytest]``/``[tool.pytest.ini_options]``,\n    which arbitrary TOML files used to be parsed with exclusively, keep\n    working; using both styles in one file is an error.\n    \"\"\"\n    document = _parse_toml_file(path)\n\n    tool_pytest_config = _config_from_tool_pytest(path, document)\n    if tool_pytest_config is None:\n        return _config_from_pytest_table(path, document)\n\n    if \"pytest\" in document:\n        raise UsageError(\n            f\"{path}: Cannot use both [pytest] and [tool.pytest]/\"\n            \"[tool.pytest.ini_options] in the same file. Please use [pytest], \"\n            \"which is what pytest's own configuration files use; the \"\n            \"[tool.pytest] tables are meant for pyproject.toml.\"\n        )\n    return tool_pytest_config\n\n\ndef _load_pyproject_toml(path: Path) -> ConfigDict | None:\n    \"\"\"Load a ``pyproject.toml``-style file.\n\n    Configuration is read from ``[tool.pytest]`` (TOML mode) or\n    ``[tool.pytest.ini_options]`` (INI mode).\n    \"\"\"\n    return _config_from_tool_pytest(path, _parse_toml_file(path))\n\n\n#: Loaders for the config files pytest discovers by name, in precedence order.","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/config/findpaths.py#L203-L239","documentation":"In a non-pyproject TOML file (e.g. pytest.toml), pytest accepts either the [pytest] table (its own native style) or the [tool.pytest]/[tool.pytest.ini_options] tables (the pyproject convention). Mixing both styles in one file is rejected because the precedence would be ambiguous and the [tool.*] tables are meant only for pyproject.toml.","triggerScenarios":"A pytest.toml containing both a [pytest] table and a [tool.pytest] (or [tool.pytest.ini_options]) table; copy-pasting a pyproject snippet into pytest.toml alongside an existing [pytest] block.","commonSituations":"Renaming pyproject.toml to pytest.toml without stripping the [tool.pytest] wrapper; merging configs from different projects.","solutions":["In pytest's own .toml files, keep only the [pytest] table and remove [tool.pytest] / [tool.pytest.ini_options].","If you want the [tool.pytest] style, rename the file to pyproject.toml and remove the [pytest] table."],"exampleFix":"// before\n[pytest]\naddopts = [\"-ra\"]\n\n[tool.pytest.ini_options]\nminversion = \"7.0\"\n// after\n[pytest]\naddopts = [\"-ra\"]\nminversion = \"7.0\"","handlingStrategy":"validation","validationCode":"import tomllib, pathlib\n\ndef validate_pytest_toml_style(path: str) -> None:\n    doc = tomllib.loads(pathlib.Path(path).read_text())\n    has_pytest = 'pytest' in doc\n    has_tool_pytest = 'pytest' in doc.get('tool', {})\n    assert not (has_pytest and has_tool_pytest), \\\n        'mixing [pytest] and [tool.pytest] in one file is not allowed'","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use [pytest] only in pytest-owned files (pytest.toml).","Reserve [tool.pytest.*] for pyproject.toml."],"tags":["config","toml","pyproject","schema"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}