python install_error ai_generated true

ModuleNotFoundError: No module named 'package' after pip install --target ./lib package

ID: python/pip-target-dir-missing-pth

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-06-28First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 90% success
    Set PYTHONPATH: export PYTHONPATH=$PWD/lib:$PYTHONPATH
  2. 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
  3. 88% success
    At runtime: import sys; sys.path.insert(0, '/abs/path/to/lib')

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Upgrade does not modify sys.path; the import still fails.

  2. 70% fail

    Works interactively but is not persisted to the deployed environment, causing the same error in CI/production.

  3. 95% fail

    --no-deps does not affect sys.path registration.