{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pytester.py#L957-L993","documentation":"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.","triggerScenarios":"Calling pytester.copy_example() (no argument) from a test whose function name does not correspond to any file or directory under pytester_example_dir.","commonSituations":"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.","solutions":["Create an example file named '{test_name}.py' or a package directory named '{test_name}' in the example dir.","Pass the explicit name: pytester.copy_example('my_example.py').","Use the @pytest.mark.pytester_example_path('subdir') marker if the example lives in a subdirectory."],"exampleFix":"// before\ndef test_feature(pytester):\n    pytester.copy_example()  # no test_feature.py exists\n// after\ndef test_feature(pytester):\n    pytester.copy_example(\"feature_example.py\")","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef example_exists(example_dir: Path, name: str) -> bool:\n    return (example_dir / name).is_file() or (example_dir / (name + \".py\")).is_file() or (example_dir / name).is_dir()\n\n# prefer passing explicit names\npytester.copy_example(\"known_example.py\")","typeGuard":null,"tryCatchPattern":"try:\n    pytester.copy_example()\nexcept LookupError as e:\n    pytest.skip(f\"example missing: {e}\")","preventionTips":["Name example files to match their test functions, or pass explicit names.","Use the pytester_example_path marker for nested example dirs.","Keep example names in sync when renaming tests."],"tags":["pytester","examples","file-not-found"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}