pytest-dev/pytest · error · UsageError

file or directory not found

Error message

file or directory not found: {arg}

What it means

The non-pyargs branch at main.py:1173 raises 'file or directory not found: {arg}' when the resolved filesystem path does not exist (safe_exists returns False) and as_pypath is False. This is the standard 'the path you gave me is not on disk' error for plain file/directory arguments.

Solutions

  1. Verify the file/dir exists with 'ls <path>' from the directory you run pytest in.
  2. Use an absolute path or change to the repo root before running.
  3. If the file was moved, update the invocation/CI config.
  4. Drop the argument to let pytest auto-collect from the default rootdir.

Example fix

// before
pytest tests/old_name.py  # renamed
// after
pytest tests/new_name.py
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
def assert_exists(arg: str):
    p = Path(arg)
    assert p.exists(), f'path not found: {arg!r} (cwd={Path.cwd()})'
    return arg

Prevention

When it happens

Trigger: Running 'pytest tests/test_foo.py' when that file does not exist; a typo in the path; running pytest from the wrong working directory; stale CI references after files moved.

Common situations: Wrong cwd when invoking pytest; renamed/moved test files; CI checkout missing the file; relative path resolved against an unexpected base.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/3f4b7831762d3ade. Report an issue: GitHub.

Appendix: 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 0d6fbdeffa)