# ImportError: attempted relative import with no known parent package

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

## Root Cause

Test modules use relative imports (from .foo import bar) but pytest imports test files as top-level modules when no __init__.py exists in the test directory. Without the package context, relative imports fail.

## Version Compatibility

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

## Workarounds

1. **** (92% success)
   ```
   touch tests/__init__.py
touch tests/unit/__init__.py
# then run from project root
pytest tests/
   ```
2. **** (95% success)
   ```
   # before: from .helpers import make_user
# after:
from tests.helpers import make_user
   ```
3. **** (85% success)
   ```
   # pyproject.toml
[tool.pytest.ini_options]
addopts = "--import-mode=importlib"
   ```

## Dead Ends

- **** — Path insertion does not create a parent package; relative imports still fail because __package__ is empty. (80% fail)
- **** — Changes rootdir discovery but does not fix the missing package marker; the relative import still has no parent. (75% fail)
