Python
pip
package installation
custom directory
dependency management

Install a Python package into a different directory using pip?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Yes. pip can install packages into a directory other than the active environment's normal site-packages location. The most direct option is --target, but whether that is the right choice depends on why you want a custom location in the first place.

Use --target for a Custom Package Directory

If you want pip to place a package into a specific folder, use --target:

bash
python3 -m pip install requests --target ./vendor

This installs requests and its dependencies into ./vendor instead of the interpreter's default package directory. That can be useful for:

  • Packaging dependencies with an application.
  • Deploying to an environment that expects a flat dependency folder.
  • Working without write access to the global interpreter.

After installation, Python must be able to find that directory at runtime. You can do that by modifying sys.path, setting PYTHONPATH, or placing the directory in a location your code already loads.

Make Python See the Installed Directory

Installing into a custom folder is only half the job. Importing from that folder is the second half.

One option is to set PYTHONPATH before running the application:

bash
export PYTHONPATH="$PWD/vendor"
python3 app.py

Another option is to add the path inside the program:

python
1import sys
2from pathlib import Path
3
4sys.path.insert(0, str(Path(__file__).parent / "vendor"))
5
6import requests
7
8print(requests.__version__)

This is runnable and works, but it should be used intentionally. Hidden path manipulation can make environments harder to reason about.

Understand the Difference Between --target, --prefix, and Virtual Environments

--target installs a package tree into one directory you specify. That is good for bundling or vendoring.

--prefix is different:

bash
python3 -m pip install requests --prefix /tmp/custom-prefix

This creates a more structured installation under a prefix directory, often including lib, bin, and related subdirectories. That is closer to a custom installation root than a flat package bundle.

A virtual environment is different again:

bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests

If your goal is project isolation, a virtual environment is usually cleaner than a custom target directory. --target is often the right answer only when you explicitly need packages outside a normal environment layout.

A Practical Vendoring Example

Suppose you want to package dependencies with a script that will run from a copied directory:

bash
mkdir -p build/vendor
python3 -m pip install rich --target build/vendor

Then the application can load from the vendored folder:

python
1import sys
2from pathlib import Path
3
4vendor_dir = Path(__file__).parent / "vendor"
5sys.path.insert(0, str(vendor_dir))
6
7from rich import print
8
9print("[bold green]Vendored import works[/bold green]")

This is a common pattern in deployment packaging, serverless bundles, and some embedded Python applications.

Use python -m pip, Not Bare pip, When It Matters

If multiple Python versions are installed, prefer:

bash
python3 -m pip install package-name --target ./vendor

That makes it explicit which interpreter owns the installation. Bare pip can silently point to the wrong Python, which is especially confusing when the custom directory later fails to import under another interpreter.

Be Aware of Dependency and Upgrade Behavior

When you install repeatedly into the same target directory, old files may remain. A second install is not a clean environment reset. If reproducibility matters, clear the directory before reinstalling or rebuild it from scratch.

That is one reason virtual environments remain the default recommendation for ordinary development. Custom target directories are powerful, but they do not automatically give you the lifecycle management that isolated environments provide.

Common Pitfalls

  • Installing with --target and forgetting to add the directory to Python's import path.
  • Using bare pip and installing with the wrong interpreter.
  • Treating a custom target directory as if it were a full virtual environment.
  • Reusing the same target folder across upgrades and accumulating stale files.
  • Choosing --target for ordinary project isolation when venv would be simpler.

Summary

  • Use python -m pip install package --target /path/to/dir to install into a custom directory.
  • Make sure Python can import from that directory at runtime.
  • Prefer --target for bundling or vendoring, not as the default replacement for virtual environments.
  • Use python -m pip to avoid interpreter mismatches.
  • Rebuild custom target directories cleanly when reproducibility matters.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.