Using pip, you can perform actions such as downloading, installing, updating, and uninstalling packages with simple commands.
Pip uses a caching mechanism that allows you to download and install Python packages faster. It works by storing a cache of the downloaded packages on the local wheel.
The caching mechanism allows pip to improve the download and installation of the packages. This is because pip does not need to download already existing packages.
In some cases, pip may not need to redownload a package when updating if it already exists in the cache.
This tutorial will discuss how to work with the pip cache, how to manage the pip cache using various commands, and how to clear it when you need to start afresh.
Pip cache command
In the recent version of pip (i.e., Pip 20.2 and above), you can use the pip cache command to manage the pip cache.
The command syntax:
Let us discuss each action below:
Pip cache dir
This command allows you to get the directory where the pip cache is stored on the target system.
The command:
The command should return the directory where the pip cache is located. Example output on Windows and Linux is as shown below:
c:\users\username\appdata\local\pip\cache
On Linux:
/home/debian/.cache/pip
Pip show cache info
You can use the pip cache info command as shown to get information about the pip cache:
Package index page cache location: /home/debian/.cache/pip/http
Package index page cache size: 0 bytes
Number of HTTP files: 0
Wheels location: /home/debian/.cache/pip/wheels
Wheels size: 0 bytes
Number of wheels: 0
The command should return a sample output as:
Pip show filenames and packages in the cache
Pip provides us with the command to view the filenames and directories stored in the cache.
Pip remove package from cache
To remove a specific package from the cache, you can use the remove action as:
The command allows you to specify a specific pattern to match a particular package.
NOTE that the specified pattern can be a glob expression or the name of a target package.
For example, to remove all the files from the cache, you can run:
Files removed: 163
NOTE that the command will remove all the files from the cache. Be careful if you do not wish to clear your cache.
Pip clean cache
You may need to reset the pip cache to default in some instances. For that, you can use the purge action as:
Pip Install package without cache
If you want to install a package without looking up the file in the pip cache, we can use the –no-cache-dir.
The command syntax is:
For example, to install TensorFlow without looking up the pip cache, run the command:
The command above should force pip to redownload the package even if it is stored in the cache.
Manually purge pip cache
If you use an older version of pip and have no access to the pip cache command, you can clear the pip cache by manually removing the cache directory.
On Linux:
$ sudo rm -rf /root/.cache/pip
On Windows:
Closing
This tutorial taught you the fundamentals of working with the pip cache. We also covered two methods to clear the pip cache depending on your installed pip version.