# ERROR collecting docs/usage.rst
E   ModuleNotFoundError: No module named 'mypkg'
# when running pytest --doctest-glob='*.rst'

- **ID:** `python/pytest-doctest-import-path`
- **Domain:** python
- **Category:** module_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Doctests in .rst files are collected from the docs directory, which is not on sys.path, so imports inside code blocks fail even though the package is installed in editable mode.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## Workarounds

1. **** (92% success)
   ```
   Set pythonpath in pytest.ini so the package is importable from any cwd:

[pytest]
pythonpath = . src
addopts = --doctest-glob='*.rst'
   ```
2. **** (90% success)
   ```
   Or install editable before running doctests:

pip install -e . && pytest --doctest-glob='*.rst' docs/
   ```

## Dead Ends

- **** — Doctest code blocks are not executable module-level statements; the insert runs but relative to the wrong cwd. (85% fail)
- **** — Installs a copy that may not reflect the working tree, causing stale doctest results. (75% fail)
