python
install_error
ai_generated
true
在 pip install --target ./lib package 之后出现 ModuleNotFoundError:没有名为 'package' 的模块
ModuleNotFoundError: No module named 'package' after pip install --target ./lib package
ID: python/pip-target-dir-missing-pth
80%修复率
86%置信度
0证据数
2025-06-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
pip install --target 将包安装到指定目录,但不会将该目录加入 sys.path,因此 Python 无法导入该包。
English
pip install --target installs into the specified directory but does not add it to sys.path, so Python cannot import the package.
解决方案
-
90% 成功率
Set PYTHONPATH: export PYTHONPATH=$PWD/lib:$PYTHONPATH
-
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
-
88% 成功率
At runtime: import sys; sys.path.insert(0, '/abs/path/to/lib')
无效尝试
常见但无效的做法:
-
90% 失败
Upgrade does not modify sys.path; the import still fails.
-
70% 失败
Works interactively but is not persisted to the deployed environment, causing the same error in CI/production.
-
95% 失败
--no-deps does not affect sys.path registration.