pytest-dev/pytest · error · LookupError

example " " is not found as a file or directory

Error message

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

What it means

Thrown by Pytester.copy_example() when an explicit `name` was passed but the resulting path is neither an existing directory nor a file. This is the fallback after the name-based joinpath found nothing on disk.

Solutions

  1. Verify the path exists under pytester_example_dir with Path.exists().
  2. Print example_dir and list its contents to confirm the expected name.
  3. Check for case mismatches and OS-specific path separators.

Example fix

// before
pytester.copy_example("MyExample.py")
// after
import pathlib
example_dir = pathlib.Path(pytester._request.config.getini('pytester_example_dir'))
print(list(example_dir.iterdir()))
pytester.copy_example("myexample.py")
Defensive patterns

Strategy: validation

Validate before calling

example_dir = pytester._request.config.rootpath / pytester._request.config.getini('pytester_example_dir')
target = example_dir / name
if not target.exists():
    pytest.fail(f'example not found: {target}. available: {list(example_dir.iterdir())}')
pytester.copy_example(name)

Prevention

When it happens

Trigger: Calling copy_example('nonexistent.py') where the file doesn't exist in example_dir; passing a relative path with wrong separators; the example was moved/deleted between writing and running the test.

Common situations: Example files renamed or relocated; CI running against a shallow checkout missing example assets; case-sensitivity mismatch on case-sensitive filesystems.

Related errors


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

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