pytest-dev/pytest · error · UsageError
file or directory not found: {arg}
Error message
file or directory not found: {arg} What it means
Raised by `resolve_collection_argument` (filesystem path mode) when the given file or directory does not exist on disk (`safe_exists(fspath)` is false). The argument is reported verbatim. This is the generic "no such path" collection error.
Source
Thrown at src/_pytest/main.py:1173
raise UsageError(f"path cannot contain [] parametrization: {arg}")
parametrization = f"{squacket}{rest}" if squacket else None
module_name = None
if as_pypath:
pyarg_strpath = search_pypath(
strpath, consider_namespace_packages=consider_namespace_packages
)
if pyarg_strpath is not None:
module_name = strpath
strpath = pyarg_strpath
fspath = invocation_path / strpath
fspath = absolutepath(fspath)
if not safe_exists(fspath):
msg = (
"module or package not found: {arg} (missing __init__.py?)"
if as_pypath
else "file or directory not found: {arg}"
)
raise UsageError(msg.format(arg=arg))
if parts and fspath.is_dir():
msg = (
"package argument cannot contain :: selection parts: {arg}"
if as_pypath
else "directory argument cannot contain :: selection parts: {arg}"
)
raise UsageError(msg.format(arg=arg))
return CollectionArgument(
path=fspath,
parts=parts,
parametrization=parametrization,
module_name=module_name,
original_index=arg_index,
)
def is_collection_argument_subsumed_by(
arg: CollectionArgument, by: CollectionArgumentView on GitHub (pinned to 98b357f69e)
Solutions
- Verify the path exists: `ls <path>` from the same cwd pytest runs in.
- Run pytest from the repository root or pass an absolute path.
- Fix typos or regenerate the missing test file.
- If using globs, ensure the shell expands them (do not quote unintended literals).
Example fix
// before $ pytest tests/test_foo.py # typo / wrong dir // after $ pytest tests/test_foo.py # after cd to repo root, or: $ pytest /abs/path/to/tests/test_foo.py
Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
def path_exists(arg: str, root: Path = Path.cwd()) -> bool:
return (root / arg).exists() Type guard
def is_existing_path(arg: str) -> bool:
from pathlib import Path
return Path(arg).exists() Prevention
- Verify paths with ls before passing them to pytest.
- Run pytest from a stable cwd (repo root).
- Use absolute paths in CI to avoid cwd drift.
When it happens
Trigger: Running `pytest tests/test_missing.py` or `pytest ./wrong_dir` where the path does not exist relative to the invocation path. Typo in path, wrong cwd, or a generated test file that was not created.
Common situations: Typo in the test path. Running pytest from the wrong working directory. CI checkout missing generated test files. Shell glob producing no matches and passing a literal pattern.
Related errors
- path cannot contain [] parametrization: {arg}
- package argument cannot contain :: selection parts: {arg}
- directory argument cannot contain :: selection parts: {arg}
- Directory '{rootdir}' not found. Check your '--rootdir' opti
- module or package not found: {arg} (missing __init__.py?)
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/3f4b7831762d3ade.json.
Report an issue: GitHub.