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
--targetinstalls flatly without asite-packages/hierarchy or interpreter binary:- Unlike a virtual environment (
python -m venv),--target ./dirdrops package folders (requests/,urllib3/,*.dist-/) directly into./dirand places console scripts inside./dir/bin(which is not on your shellPATH). Use--targetfor AWS Lambda/Cloud Functions deployment bundles, plugin folders, or vendored directories—not as a replacement forvenv.
- Unlike a virtual environment (
- Upgrading or overwriting packages already inside
--target(--upgrade):pipdoes not automatically detect pre-existing packages inside a custom--targetfolder cleanly unless you pass--upgrade(python -m pip install --target ./custom_libs --upgrade requests), which replaces existing package directories instead of warningTarget 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
--targetwith 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
- When packaging dependencies on macOS or Windows for a Linux serverless target (such as AWS Lambda), combine
- Conflict between
--targetand--user(--no-user):- If your system or global
pip.confsetsuser = true(common on managed Linux distros),pip install --targetfails withERROR: Can not combine '--user' and '--target'. Pass--no-user(python -m pip install --no-user --target ./custom_libs <pkg>) to override it.
- If your system or global
- Alternative when you want a full
lib/pythonX.Y/site-packagesprefix layout (--prefix):- If you are building system packages or custom installation prefixes that expect standard
bin/andlib/python3.X/site-packages/subdirectories rather than a flat directory, pass--prefix /custom/prefixinstead of--target.
- If you are building system packages or custom installation prefixes that expect standard
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. |
You must log in or register to comment.
