{"record":{"id":"ef19f51e33dd4a86","repo":"pytest-dev/pytest","slug":"func-name-can-t-be-found-as-module-or-package-in","errorCode":null,"errorMessage":"{func_name} can't be found as module or package in {example_dir}","messagePattern":"(.+?) can't be found as module or package in (.+?)","errorType":"exception","errorClass":"LookupError","httpStatus":null,"severity":"error","filePath":"src/_pytest/pytester.py","lineNumber":975,"sourceCode":"        if example_dir_ is None:\n            raise ValueError(\"pytester_example_dir is unset, can't copy examples\")\n        example_dir: Path = self._request.config.rootpath / example_dir_\n\n        for extra_element in self._request.node.iter_markers(\"pytester_example_path\"):\n            assert extra_element.args\n            example_dir = example_dir.joinpath(*extra_element.args)\n\n        if name is None:\n            func_name = self._name\n            maybe_dir = example_dir / func_name\n            maybe_file = example_dir / (func_name + \".py\")\n\n            if maybe_dir.is_dir():\n                example_path = maybe_dir\n            elif maybe_file.is_file():\n                example_path = maybe_file\n            else:\n                raise LookupError(\n                    f\"{func_name} can't be found as module or package in {example_dir}\"\n                )\n        else:\n            example_path = example_dir.joinpath(name)\n\n        if example_path.is_dir() and not example_path.joinpath(\"__init__.py\").is_file():\n            shutil.copytree(example_path, self.path, symlinks=True, dirs_exist_ok=True)\n            return self.path\n        elif example_path.is_file():\n            result = self.path.joinpath(example_path.name)\n            shutil.copy(example_path, result)\n            return result\n        else:\n            raise LookupError(\n                f'example \"{example_path}\" is not found as a file or directory'\n            )\n\n    def getnode(self, config: Config, arg: str | os.PathLike[str]) -> Collector | Item:","sourceCodeStart":957,"sourceCodeEnd":993,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pytester.py#L957-L993","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Create either `<test_func_name>.py` or `<test_func_name>/__init__.py` under pytester_example_dir.","Pass an explicit `name` argument to copy_example() pointing at the actual file/dir.","Add a `pytester_example_path` marker on the test to relocate the search dir."],"exampleFix":"// before\ndef test_feature(pytester):\n    pytester.copy_example()  # looks for test_feature.py\n// after\ndef test_feature(pytester):\n    pytester.copy_example(\"feature_demo.py\")","handlingStrategy":"validation","validationCode":"import pathlib\nexample_dir = pathlib.Path(pytester._request.config.getini('pytester_example_dir'))\ncandidates = [example_dir / 'test_feature', example_dir / 'test_feature.py']\nif not any(p.exists() for p in candidates):\n    pytest.skip('no example artifact for this test')\npytester.copy_example()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Name example files to match the test function name, or pass explicit name.","Keep example artifacts in lockstep with test renames.","Use pytester_example_path marker for nested example layouts."],"tags":["pytester","copy-example","lookup","filesystem"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}