pytest-dev/pytest · error · LookupError

example "{example_path}" is not found as a file or directory

Error message

example "{example_path}" is not found as a file or directory

What it means

Raised by Pytester.copy_example when an explicit name argument was given but the resulting path under example_dir is neither an existing file nor an existing directory. This is the explicit-name counterpart to error 147; the path was constructed but does not resolve to anything on disk.

Source

Thrown at src/_pytest/pytester.py:989

                example_path = maybe_dir
            elif maybe_file.is_file():
                example_path = maybe_file
            else:
                raise LookupError(
                    f"{func_name} can't be found as module or package in {example_dir}"
                )
        else:
            example_path = example_dir.joinpath(name)

        if example_path.is_dir() and not example_path.joinpath("__init__.py").is_file():
            shutil.copytree(example_path, self.path, symlinks=True, dirs_exist_ok=True)
            return self.path
        elif example_path.is_file():
            result = self.path.joinpath(example_path.name)
            shutil.copy(example_path, result)
            return result
        else:
            raise LookupError(
                f'example "{example_path}" is not found as a file or directory'
            )

    def getnode(self, config: Config, arg: str | os.PathLike[str]) -> Collector | Item:
        """Get the collection node of a file.

        :param config:
           A pytest config.
           See :py:meth:`parseconfig` and :py:meth:`parseconfigure` for creating it.
        :param arg:
            Path to the file.
        :returns:
            The node.
        """
        session = Session.from_config(config)
        assert "::" not in str(arg)
        p = Path(os.path.abspath(arg))
        config.hook.pytest_sessionstart(session=session)

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Verify the exact filename exists under the configured pytester_example_dir.
  2. Correct the name argument to match the actual file/directory path.
  3. Ensure example files are committed and included in CI artifacts/checkouts.

Example fix

// before
pytester.copy_example("featue.py")  # typo
// after
pytester.copy_example("feature.py")
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

example_root = Path(pytestconfig.rootpath) / pytestconfig.getini("pytester_example_dir")
target = example_root / name
assert target.is_file() or target.is_dir(), f"{target} does not exist"
pytester.copy_example(name)

Try / catch

try:
    pytester.copy_example(name)
except LookupError as e:
    raise AssertionError(f"example {name!r} not found under example dir: {e}") from e

Prevention

When it happens

Trigger: Calling pytester.copy_example('nonexistent.py') where that file does not exist relative to example_dir; passing a relative path that does not resolve; the example was deleted or never committed.

Common situations: Typo in the example name; renaming/moving example files without updating test references; examples excluded from the VCS checkout or Docker context.

Related errors


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