You can install a Python package in another directory by specifying the target installation path by using pip and the --target option. Here's how you do it:
- Open your terminal or command prompt.
- Run the pip install command with the --target option. Replace your_package with the name of the package you want to install and path/to/directory with the installation path you have selected:
pip install your_package --target path/to/directory
For using the Installed Package:
You must now let Python find the package once installed into a different directory. One way to do that is to add the directory in question to your PYTHONPATH environment variable. You may also modify your script so it looks like this:
import sys
sys.path.append('path/to/directory')
import requests # Now you can use the package