pytest-dev/pytest · error · LookupError

can't be found as module or package in

Error message

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

What it means

Thrown by Pytester.copy_example() when name is None and neither `<test_name>/` directory nor `<test_name>.py` file exists in the example dir. The lookup is by the requesting test function's name.

Solutions

  1. Create either `<test_func_name>.py` or `<test_func_name>/__init__.py` under pytester_example_dir.
  2. Pass an explicit `name` argument to copy_example() pointing at the actual file/dir.
  3. Add a `pytester_example_path` marker on the test to relocate the search dir.

Example fix

// before
def test_feature(pytester):
    pytester.copy_example()  # looks for test_feature.py
// after
def test_feature(pytester):
    pytester.copy_example("feature_demo.py")
Defensive patterns

Strategy: validation

Validate before calling

import pathlib
example_dir = pathlib.Path(pytester._request.config.getini('pytester_example_dir'))
candidates = [example_dir / 'test_feature', example_dir / 'test_feature.py']
if not any(p.exists() for p in candidates):
    pytest.skip('no example artifact for this test')
pytester.copy_example()

Prevention

When it happens

Trigger: Calling copy_example() with no args from a test method `test_x` when there is no `test_x/` dir or `test_x.py` file in pytester_example_dir; renaming the test without renaming the example artifact; using a pytester_example_path marker that points at the wrong subdir.

Common situations: Renaming test functions during refactoring and forgetting to rename the example; example file located at a different nesting level than expected; typo in the marker args.

Related errors


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

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