# 在 pip install --target ./lib package 之后出现 ModuleNotFoundError：没有名为 'package' 的模块

- **ID:** `python/pip-target-dir-missing-pth`
- **领域:** python
- **类别:** install_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pip install --target 将包安装到指定目录，但不会将该目录加入 sys.path，因此 Python 无法导入该包。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Set PYTHONPATH: export PYTHONPATH=$PWD/lib:$PYTHONPATH
   ```
2. **** (85% 成功率)
   ```
   Add a .pth file in site-packages pointing to the target: echo "/abs/path/to/lib" > $(python -c 'import site; print(site.getsitepackages()[0])')/custom.pth
   ```
3. **** (88% 成功率)
   ```
   At runtime: import sys; sys.path.insert(0, '/abs/path/to/lib')
   ```

## 无效尝试

- **** — Upgrade does not modify sys.path; the import still fails. (90% 失败率)
- **** — Works interactively but is not persisted to the deployed environment, causing the same error in CI/production. (70% 失败率)
- **** — --no-deps does not affect sys.path registration. (95% 失败率)
