Change Pip Install Location

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 in 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.

Method 1: Through the terminal/ command line

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,

Figure 1: pip installs NumPy on the specific folder (left), and on the right, the installed NumPy package with its dependencies is displayed.

Method 2: Editing pip Configuration file

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/.config/pip/pip.conf

Windows

%HOME%\pip\pip.ini

$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.

Example (Linux)

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 the 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:

[global]
target=<path/to/install/packages>

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:

[global]
target=~/Desktop/test_modules2

On installing Python packages now, they are installed on the above directory. See below.

Figure 2: pip now installs packages on the directory defined on the configuration file. In this example, pip installs pandas and their dependencies in ~/Desktop/test_modules2.

Example (Windows)

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:

[global]
target=<target_dir>

Where <target_dir> is the default directory for pip installation going forward unless you change the settings on the configuration file.

Figure 3: After setting as C:\Users\kipro\test, pip now installs numpy on the target directory.

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 in the pip documentation.

Removing Packages Installed Using the Above Methods

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.

(Important) Remark on Usage of Modules Installed as Above

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 with the above methods, see this article.