Python-pip, by default, installs packages on a global scope – for all users. In Unix-based systems, the packages are installed on /usr/local/bin/ whereas, on Windows, they are located on the Program Files directory.
This article focuses on the cases we are interested in installing Python packages in a specific folder. We will also explain how to use and remove packages installed in this manner.
There are two ways to install packages to a particular directory – through the terminal (command line) and by editing the pip configuration file.
When installing the modules using pip on the terminal or Windows PowerShell, we have a “target” option or short-hand “t” that allows us to specify the directory to which we want packages to be installed. The general syntax is as shown below
pip install -target=<target_dir> <package_name>
Here is an example of how I installed NumPy to the “test_modules” folder on my Desktop,
You can also specify the default pip installation location in the configuration file, which is located in the following path based on the OS (you may have to create these paths and files):
Unix and Mac OS
$HOME is the home directory of the current user on Linux/Mac, usually located at /home/<username>. It can also be written as a tilde (~). On Windows, the home directory %HOME% is located in C:\Users\<username> for the logged-in user. Like in Linux, the ~ also means home folder when using WindowsPowerShell.
At the start, the directory does not exist, so I have to create the pip directory and add the configuration file pip.config inside it. Executed the following command on terminal:
mkdir -p ~/.config/pip && touch ~/.config/pip/pip.conf
(“p” option allows us to create a folder within a folder if there’s a need, whereas && enables us to join two commands into one line.).
That creates an empty configuration file. On the file, we define the default installation location with the following general syntax:
In our example, we want to install packages on the “test_modules2″ directory on the Desktop. The file ~/.config/pip/pip.config will have the following contents:
On installing Python packages now, they are installed on the above directory. See below.
First of all, we need the pip.ini file. To get that, we run the following command on Windows PowerShell (of course, you can also create files and folders on the GUI):
mkdir ~\pip ; cd > ~\pip\pip.ini
The semi-colon (;) is used to combine two commands in one line, and cd > creates an empty file, that is to say, the command cd > ~\pip\pip.ini in our case creates a pip.ini file within the pip folder in the home directory (C:\Users\<username>).
Next, we need to specify the target directory in the configuration file pip.ini. For that, we add the following content to the configuration file:
Where <target_dir> is the default directory for pip installation going forward unless you change the settings on the configuration file.
Note: If the target directory indicated on the config file does not exist, it is created. You can read more about the pip configuration file on the pip documentation.
To remove the packages installed in the above methods, delete the folders with the installed package(s). Remember to remove the settings on the configuration file if you no longer need them and want to go to default.
After setting the default pip installation location, Python may continue looking for installed modules on the default paths. If you want to learn how to import packages from the location we just created in the above methods, see this article.