pytest-dev/pytest · error · ValueError
pytester_example_dir is unset, can't copy examples
Error message
pytester_example_dir is unset, can't copy examples
What it means
Thrown by Pytester.copy_example() when the `pytester_example_dir` ini option is not set. copy_example() needs a project-level directory to find example files; without it there is nowhere to copy from.
Solutions
- Add `pytester_example_dir = example` (or your dir) under [pytest] in pytest.ini / pyproject.toml / tox.ini.
- If examples live elsewhere, point pytester_example_dir at the correct relative path from rootdir.
- Skip the test when the ini is unset: pytest.importorskip or a custom skip guard.
Example fix
// before - pytest.ini missing the key [pytest] addopts = -ra // after [pytest] addopts = -ra pytester_example_dir = examples
Defensive patterns
Strategy: validation
Validate before calling
example_dir = pytester._request.config.getini('pytester_example_dir')
if example_dir is None:
pytest.skip('pytester_example_dir not configured')
pytester.copy_example() Prevention
- Add pytester_example_dir to [pytest] in pytest.ini when using copy_example.
- Skip tests that require examples when the ini is unset.
- Document the ini dependency in the test module docstring.
When it happens
Trigger: Calling pytester.copy_example() in a test where pytest.ini/tox.ini/setup.cfg does not define pytester_example_dir; running tests in a downstream project that reuses your test suite but didn't carry over the ini setting.
Common situations: Copy-pasting a pytester test that uses copy_example into a new project without adding the ini key; upgrading pytest and forgetting that the ini option name must match exactly.
Related errors
- example " " is not found as a file or directory
- can't be found as module or package in
- Unknown parametrize_long_str_id_strategy
- alias conflicts with existing configuration option
- is already an alias of
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/26e09bd019fc271b.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/pytester.py:958
gets recognised as a Python package.
"""
p = self.path / name
p.mkdir()
p.joinpath("__init__.py").touch()
return p
def copy_example(self, name: str | None = None) -> Path:
"""Copy file from project's directory into the testdir.
:param name:
The name of the file to copy.
:return:
Path to the copied directory (inside ``self.path``).
:rtype: pathlib.Path
"""
example_dir_ = self._request.config.getini("pytester_example_dir")
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}"View on GitHub (pinned to 0d6fbdeffa)