# ERROR collecting tests/test_api.py
ImportError while importing test module 'tests/test_api.py'.
ModuleNotFoundError: No module named 'myapp'

- **ID:** `python/pytest-collection-import-error`
- **Domain:** python
- **Category:** install_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

pytest adds the rootdir to sys.path only in certain modes. When invoked as 'pytest tests/' from a subdirectory or with a src layout, the package root is not on sys.path.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   pip install -e .
# then run: pytest
   ```
2. **** (90% success)
   ```
   # pytest.ini
[pytest]
pythonpath = src
   ```

## Dead Ends

- **** — The CWD is not always the project root, and sys.path hacks break when tests run from CI with a different working directory. (60% fail)
- **** — This works only if the CWD is the project root; under src layout it still fails because the package lives under src/. (50% fail)
