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: CollectionArgument

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Verify the path exists: `ls <path>` from the same cwd pytest runs in.
  2. Run pytest from the repository root or pass an absolute path.
  3. Fix typos or regenerate the missing test file.
  4. 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

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


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/3f4b7831762d3ade.json. Report an issue: GitHub.