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.
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:
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:
Another option is to add the path inside the program:
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:
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:
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:
Then the application can load from the vendored folder:
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:
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
--targetand forgetting to add the directory to Python's import path. - Using bare
pipand 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
--targetfor ordinary project isolation whenvenvwould be simpler.
Summary
- Use
python -m pip install package --target /path/to/dirto install into a custom directory. - Make sure Python can import from that directory at runtime.
- Prefer
--targetfor bundling or vendoring, not as the default replacement for virtual environments. - Use
python -m pipto avoid interpreter mismatches. - Rebuild custom target directories cleanly when reproducibility matters.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.