GitHub Actions pip Cache Not Working for Python Projects

Issue

In GitHub Actions workflows for Python projects, pip install times are slow and it appears that the pip cache is not being used between workflow runs. Even when using the actions/cache step, dependencies are reinstalled from scratch. How can pip caching be enabled and made effective in GitHub Actions?

Solution

To enable pip cache in GitHub Actions, you need to cache the pip cache directory. For most systems, this is ~/.cache/pip. Here is an example of how to do this:

- name: Cache pip
  uses: actions/cache@v4
  with:
    path: ~/.cache/pip
    key: $-pip-$
    restore-keys: |
      $-pip-

- name: Install dependencies
  run: pip install -r requirements.txt

Make sure the cache key includes a hash of your requirements file. This will ensure the cache is updated when dependencies change. If you use pip install --no-cache-dir, remove that flag to allow caching.