
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to install a Python package into a different directory using pip?
In Python, pip is a standard tool which is used to install third-party packages in our system. By default, pip installs packages into the site-packages directory of the current Python environment but in some cases, such as restricted environments or creating portable apps, we may need to install packages into a different directory. Then we can use the pip's --target option.
Syntax
Following is the syntax of using the pip command while installing a python package in a different directory -
pip install <package-name> --target <custom-directory>
The above command tells pip to install the specified package and all its dependencies in the given directory.
In this article, we'll explore several methods to install Python packages into a different directory using pip.
- Using the ??target Option with pip
- Setting the PYTHONPATH Environment Variable
- Using a Virtual Environment
Using the ??target Option with pip
The --target option in pip is used to install a Python package into a specific directory instead of default system-wide or virtual environment location. This option in useful when we don't have permission to install packages globally.
Example
Following is the example in which we are installing the requests module in the ./my_libs specified directory by using --target in pip -
pip install requests --target ./my_libs
Following is the output of the above command -
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) Downloading requests-2.32.4-py3-none-any.whl (64 kB) Downloading certifi-2025.4.26-py3-none-any.whl (159 kB) Downloading charset_normalizer-3.4.2-cp312-cp312-win_amd64.whl (105 kB) Downloading idna-3.10-py3-none-any.whl (70 kB) Downloading urllib3-2.4.0-py3-none-any.whl (128 kB) Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests Successfully installed certifi-2025.4.26 charset_normalizer-3.4.2 idna-3.10 requests-2.32.4 urllib3-2.4.0
Setting the PYTHONPATH Environment Variable
The PYTHONPATH environment variable is used to add additional directories where Python will look for modules and packages. This is helpful when packages are installed in a custom directory using options such as --target and we want Python to automatically detect them without modifying sys.path in every script.
Syntax
Below is the syntax of setting PYTHONPATH environment Variable in the ./my_libs directory on Linux/macOS systems -
export PYTHONPATH="./my_libs:$PYTHONPATH"
The Syntax to set the PYTHONPATH on Windows is slightly different from Linux/macOS and given as follows -
set PYTHONPATH=.\my_libs;%PYTHONPATH%
Example
Following is the example of setting the PYTHONPATH environment Variable to add the Numpy package to our custom directory by using the export PYTHONPATH command -
set PYTHONPATH=~/custom_lib:$PYTHONPATH pip install numpy
Using a Virtual Environment
A virtual environment in Python is an isolated environment that allows us to install packages locally for a specific project without affecting the global Python installation. It help us to manage dependencies and avoid version conflicts between projects.
Following is the example to create a virtual environment named venv using the venv module -
python -m venv venv
After creating the virtual environment we have to activate it using the following command based on our operating system -
On Windows:
venv\Scripts\activate
On Linux/macOS:
source venv/bin/activate
Once activated the Virtual environment is activated then any packages we install using pip will be installed into the virtual environment instead of the global Python environment. For example:
pip install requests
To deactivate the virtual environment we can simply run the following command in the command prompt-
deactivate