Pip is a command line utility for Python that makes it easy to manage Python packages. With Pip, you can download, install, update, and uninstall packages using simple commands thus making it very efficient.
As efficient as Pip is, it uses a caching mechanism to improve its functionality. You will find that after using it for some time, it accumulates a cache of downloaded packages and metadata, which in turn takes up valuable disk space and introduces potential issues due to outdated package information.
In this article, you will learn how to clear Pip Cache to ensure that you are not using outdated packages and save some disk space.
How Pip Cache Works
As mentioned above, Pip uses a caching system to ensure quick and efficient download and installation of Python packages. For this to be possible, Pip will store the downloaded packages locally thus ensuring that subsequent downloads are accelerated.
By storing the packages locally, Pip will avoid redownloading packages that are already in the cache to ensure that the updates are more efficient.
How to Use Pip Cache
The syntax for using Pip cache is as follows:
pip cache <action>
Replace with either of the following:
dir, info, list, remove, purge depending on what you want to achieve.
Let's see it in action.
Note: Windows OS is used for this demo.
Pip Cache Dir
dir as the action, you should see the path to the cache directory. Here is an example:
pip cache dir
With this directory path, you can head there and delete the cache folder manually.
Pip Cache info
Other than getting the cache path directory, you can also get more information about the cache such as the space it occupies, the number of files, and wheels. Let's see it in action.
pip cache info
The built wheel is a binary package format that holds the pre-compiled code and other necessary files for the installation of a Python package. They just serve the purpose of speeding up the updates and installation.
Pip Cache list
You can also get a list of all the package filenames that are stored in the cache. Use the command below for that.
pip cache list
Pip Cache remove
You can also remove a particular cache package or the entire cache files. Here is how to do it.
pip cache remove [package_name]
Replace [package_name] with the package you want to remove. For example:
pip cache remove python_nmap
By running the command above, the python nmap package is removed and if we run the
pip cache listcommand, we see that the nmap package is missing.
You can also remove all the cache packages by using the
* in place of the package name as shown below.
pip cache remove *
For older versions of Pip, you can use the purge command instead
Pip Cache Purge
The purge action does the same thing as
pip cache remove *. It clears the entire cache.
pip cache purge
Manually Clearing Pip Cache
Alternatively, you can delete the pip cache by heading to its path directory and deleting that folder. As shown above, you can get the pip cache path directory by running the following command.
pip cache dir
Once you get the directory path, navigate to that directory and manually delete the files and folders you no longer need.
Note: Be careful not to delete important files that are still in use
Install Packages without using Cache
One of the reasons you delete cache is so that your download is not outdated. You can clear the cache using the methods above. Apart from deleting the cache, you can also install packages and ignore caching. Here is how to do so. We are going to install Basemap in this example.
pip install basemap --no-cache-dir
By using this command, Pip is forced to redownload the package and the dependencies required even though they are available in the cache.
See how to use Basemap in detail in this tutorial.
This guide explains how you can use pip to delete cache. You also learn how to use the
pip cache actions. Pip is made efficient using the caching mechanism but this introduces a problem with outdated packages and disk space. You now know how to clear Pip cache.
Thank you for reading : )
Learn how you can clear the git cache in this tutorial.