Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to install a Python package into a different directory using pip?
In Python, pip is a standard tool used to install third-party packages. By default, pip installs packages into the site-packages directory of the current Python environment. However, in some cases, such as restricted environments or creating portable applications, you may need to install packages into a different directory using pip's --target option.
Syntax
The basic syntax for installing a Python package in a custom directory is ?
pip install <package-name> --target <custom-directory>
This command tells pip to install the specified package and all its dependencies in the given directory.
In this article, we'll explore three methods to install Python packages into different directories:
- Using the --target Option with pip
- Setting the PYTHONPATH Environment Variable
- Using a Virtual Environment
Method 1: Using the --target Option with pip
The --target option allows you to install a Python package into a specific directory instead of the default system-wide location. This is particularly useful when you don't have permission to install packages globally.
Example
Let's install the requests module in a custom directory called my_libs ?
pip install requests --target ./my_libs
The output will show the installation progress ?
Collecting requests Downloading requests-2.32.4-py3-none-any.whl.metadata (4.9 kB) Collecting charset_normalizer<4,>=2 (from requests) Downloading charset_normalizer-3.4.2-cp312-cp312-win_amd64.whl.metadata (36 kB) Collecting idna<4,>=2.5 (from requests) Downloading idna-3.10-py3-none-any.whl.metadata (10 kB) Collecting urllib3<3,>=1.21.1 (from requests) Downloading urllib3-2.4.0-py3-none-any.whl.metadata (6.5 kB) Collecting certifi>=2017.4.17 (from requests) Downloading certifi-2025.4.26-py3-none-any.whl.metadata (2.5 kB) Successfully installed certifi-2025.4.26 charset_normalizer-3.4.2 idna-3.10 requests-2.32.4 urllib3-2.4.0
After installation, you can use the package by adding the custom directory to Python's path ?
import sys
sys.path.insert(0, './my_libs')
import requests
response = requests.get('https://httpbin.org/get')
print(f"Status Code: {response.status_code}")
Status Code: 200
Method 2: Setting the PYTHONPATH Environment Variable
The PYTHONPATH environment variable tells Python where to look for modules and packages. This is helpful when packages are installed in custom directories and you want Python to automatically detect them.
Syntax
On Linux/macOS:
export PYTHONPATH="./my_libs:$PYTHONPATH"
On Windows:
set PYTHONPATH=.\my_libs;%PYTHONPATH%
Example
First, install a package to a custom directory ?
pip install numpy --target ./custom_lib
Then set the PYTHONPATH and use the package ?
export PYTHONPATH="./custom_lib:$PYTHONPATH"
Now you can import the package directly in your Python script ?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(f"Array: {arr}")
print(f"Sum: {np.sum(arr)}")
Array: [1 2 3 4 5] Sum: 15
Method 3: Using a Virtual Environment
A virtual environment creates an isolated Python environment that allows you to install packages locally for a specific project without affecting the global Python installation.
Creating and Activating a Virtual Environment
Create a virtual environment named myenv ?
python -m venv myenv
Activate the virtual environment based on your operating system ?
On Windows:
myenv\Scripts\activate
On Linux/macOS:
source myenv/bin/activate
Once activated, install packages normally ?
pip install requests pandas
To deactivate the virtual environment ?
deactivate
Comparison of Methods
| Method | Use Case | Isolation Level | Complexity |
|---|---|---|---|
--target |
Portable apps, restricted environments | Low | Simple |
PYTHONPATH |
System-wide custom locations | Low | Medium |
| Virtual Environment | Project isolation | High | Simple |
Conclusion
Use --target for quick custom installations, PYTHONPATH for system-wide custom paths, and virtual environments for project isolation. Virtual environments are generally recommended for most development scenarios as they provide the best dependency management and isolation.
