How do you install a Python package into a custom target directory using pip? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you install a Python package into a custom target directory using pip?

message-square
0

Pass the --target <directory> (or -t <directory>) flag to python -m pip install—for example, python -m pip install --target ./vendor requests—to unpack the package and its dependencies directly into that folder, and add that folder to sys.path or PYTHONPATH at runtime so Python can import it (or use python -m venv <dir> instead if you want an isolated environment with its own Python executable and CLI entry points).

Basic Example

# 1. Recommended: Install a package and its dependencies into a custom directory
python -m pip install --target ./custom_libs requests

# 2. Run a script with that custom directory added to Python's import search path:
PYTHONPATH=./custom_libs python app.py
# 3. Or add the custom directory dynamically inside Python before importing:
import sys
from pathlib import Path

custom_dir = Path(__file__).resolve().parent / "custom_libs"
sys.path.insert(0, str(custom_dir))

import requests
print(requests.__file__)
# Output: /path/to/project/custom_libs/requests/__init__.py

Key Details & Edge Cases

  • --target installs flatly without a site-packages/ hierarchy or interpreter binary:
    • Unlike a virtual environment (python -m venv), --target ./dir drops package folders (requests/, urllib3/, *.dist-info/) directly into ./dir and places console scripts inside ./dir/bin (which is not on your shell PATH). Use --target for AWS Lambda/Cloud Functions deployment bundles, plugin folders, or vendored directories—not as a replacement for venv.
  • Upgrading or overwriting packages already inside --target (--upgrade):
    • pip does not automatically detect pre-existing packages inside a custom --target folder cleanly unless you pass --upgrade (python -m pip install --target ./custom_libs --upgrade requests), which replaces existing package directories instead of warning Target directory ... already exists.
  • Cross-platform builds for Linux deployment bundles (--platform, --only-binary=:all:, --python-version):
    • When packaging dependencies on macOS or Windows for a Linux serverless target (such as AWS Lambda), combine --target with target-platform wheel flags so C extensions match the deployment runtime:
      python -m pip install \
        --target ./lambda_package \
        --platform manylinux2014_x86_64 \
        --python-version 3.12 \
        --only-binary=:all: \
        numpy pydantic
      
  • Conflict between --target and --user (--no-user):
    • If your system or global pip.conf sets user = true (common on managed Linux distros), pip install --target fails with ERROR: Can not combine '--user' and '--target'. Pass --no-user (python -m pip install --no-user --target ./custom_libs <pkg>) to override it.
  • Alternative when you want a full lib/pythonX.Y/site-packages prefix layout (--prefix):
    • If you are building system packages or custom installation prefixes that expect standard bin/ and lib/python3.X/site-packages/ subdirectories rather than a flat directory, pass --prefix /custom/prefix instead of --target.

Comparison of Custom-Location Installation Flags in pip

Flag / Command Directory Layout Created Includes Isolated Python Binary & activate? Best Used When
python -m pip install --target ./dir <pkg> (Recommended for flat bundles) Flat (./dir/<pkg>/, ./dir/bin/) No (requires PYTHONPATH or sys.path) Serverless deployment zips (AWS Lambda), vendored plugin folders, or embedded Python apps.
python -m venv ./dir Full virtual environment (./dir/bin/python, site-packages/) Yes Local project development, testing, and isolating project dependencies cleanly.
python -m pip install --prefix ./dir <pkg> Standard Unix/Windows prefix (./dir/bin/, ./dir/lib/pythonX.Y/site-packages/) No Custom OS packaging, container multi-stage copies, or GNU-style prefix installations.
python -m pip install --user <pkg> User home site-packages (~/.local/lib/pythonX.Y/site-packages/) No Installing user-level CLI tools globally without sudo when not inside a virtual environment.