python
install_error
ai_generated
true
ModuleNotFoundError: No module named 'package' after pip install --target ./lib package
ID: python/pip-target-dir-missing-pth
80%Fix Rate
86%Confidence
0Evidence
2025-06-28First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
pip install --target installs into the specified directory but does not add it to sys.path, so Python cannot import the package.
generic中文
pip install --target 将包安装到指定目录,但不会将该目录加入 sys.path,因此 Python 无法导入该包。
Workarounds
-
90% success
Set PYTHONPATH: export PYTHONPATH=$PWD/lib:$PYTHONPATH
-
85% success
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% success
At runtime: import sys; sys.path.insert(0, '/abs/path/to/lib')
Dead Ends
Common approaches that don't work:
-
90% fail
Upgrade does not modify sys.path; the import still fails.
-
70% fail
Works interactively but is not persisted to the deployed environment, causing the same error in CI/production.
-
95% fail
--no-deps does not affect sys.path registration.