{"id":"4048d65d101874bb","repo":"pytest-dev/pytest","slug":"optname-must-be-a-directory-given-path","errorCode":null,"errorMessage":"{optname} must be a directory, given: {path}","messagePattern":"(.+?) must be a directory, given: (.+?)","errorType":"validation","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":308,"sourceCode":"def filename_arg(path: str, optname: str) -> str:\n    \"\"\"Argparse type validator for filename arguments.\n\n    :path: Path of filename.\n    :optname: Name of the option.\n    \"\"\"\n    if os.path.isdir(path):\n        raise UsageError(f\"{optname} must be a filename, given: {path}\")\n    return path\n\n\ndef directory_arg(path: str, optname: str) -> str:\n    \"\"\"Argparse type validator for directory arguments.\n\n    :path: Path of directory.\n    :optname: Name of the option.\n    \"\"\"\n    if not os.path.isdir(path):\n        raise UsageError(f\"{optname} must be a directory, given: {path}\")\n    return path\n\n\n# Plugins that cannot be disabled via \"-p no:X\" currently.\nessential_plugins = (\n    \"mark\",\n    \"main\",\n    \"runner\",\n    \"fixtures\",\n    \"helpconfig\",  # Provides -p.\n)\n\ndefault_plugins = (\n    *essential_plugins,\n    \"python\",\n    \"terminal\",\n    \"debugging\",\n    \"unittest\",","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/config/__init__.py#L290-L326","documentation":"The directory_arg function is an argparse type validator for pytest CLI options (e.g., --rootdir, -o with certain args) that must receive a directory path. If the path does not exist or is a file rather than a directory, pytest raises UsageError. This ensures pytest operates on an existing directory.","triggerScenarios":"Passing a nonexistent path or a file path to an option that expects a directory (wired through directory_arg). os.path.isdir(path) returns False, triggering the error.","commonSituations":"Typo in the directory name, pointing at a path that hasn't been created yet, or pointing at a file (e.g., conftest.py) instead of its parent directory.","solutions":["Create the directory first if it does not exist (mkdir -p <path>).","Correct the path to point at an existing directory, not a file.","Use an absolute path to avoid cwd-relative resolution ambiguity."],"exampleFix":"# before\npytest --rootdir ./conftest.py\n\n# after\npytest --rootdir ./tests","handlingStrategy":"validation","validationCode":"import os\n\ndef ensure_is_directory(path: str, optname: str) -> str:\n    if not os.path.isdir(path):\n        raise FileNotFoundError(f\"{optname} must be a directory, but '{path}' is not a directory\")\n    return path","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create directories with os.makedirs(path, exist_ok=True) before referencing them in CLI options.","Validate directory existence in setup scripts before invoking pytest."],"tags":["config","cli","argparse","filesystem"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}