pytest-dev/pytest · error · LookupError

{func_name} can't be found as module or package in {example_

Error message

{func_name} can't be found as module or package in {example_dir}

What it means

Raised by Pytester.copy_example when no explicit name is given and neither a directory nor a .py file named after the test function exists in the example_dir. copy_example derives a default name from the requesting test function (_name) and looks for either a same-named package directory or a '{name}.py' module; if neither is present it raises LookupError.

Source

Thrown at src/_pytest/pytester.py:975

        if example_dir_ is None:
            raise ValueError("pytester_example_dir is unset, can't copy examples")
        example_dir: Path = self._request.config.rootpath / example_dir_

        for extra_element in self._request.node.iter_markers("pytester_example_path"):
            assert extra_element.args
            example_dir = example_dir.joinpath(*extra_element.args)

        if name is None:
            func_name = self._name
            maybe_dir = example_dir / func_name
            maybe_file = example_dir / (func_name + ".py")

            if maybe_dir.is_dir():
                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:

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Create an example file named '{test_name}.py' or a package directory named '{test_name}' in the example dir.
  2. Pass the explicit name: pytester.copy_example('my_example.py').
  3. Use the @pytest.mark.pytester_example_path('subdir') marker if the example lives in a subdirectory.

Example fix

// before
def test_feature(pytester):
    pytester.copy_example()  # no test_feature.py exists
// after
def test_feature(pytester):
    pytester.copy_example("feature_example.py")
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

def example_exists(example_dir: Path, name: str) -> bool:
    return (example_dir / name).is_file() or (example_dir / (name + ".py")).is_file() or (example_dir / name).is_dir()

# prefer passing explicit names
pytester.copy_example("known_example.py")

Try / catch

try:
    pytester.copy_example()
except LookupError as e:
    pytest.skip(f"example missing: {e}")

Prevention

When it happens

Trigger: Calling pytester.copy_example() (no argument) from a test whose function name does not correspond to any file or directory under pytester_example_dir.

Common situations: Renaming a test function but forgetting to rename the matching example file/dir; examples stored under a subdirectory without using the pytester_example_path marker; typo in the example filename.

Related errors


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