setup-python icon indicating copy to clipboard operation
setup-python copied to clipboard

What if I need to cache both pip and pipenv, is that possible?

Open nabheet opened this issue 1 year ago • 1 comments

I would like to cache both pip and pipenv. Wondering how I would accomplish that.

nabheet avatar Aug 29 '24 20:08 nabheet

Hello @nabheet, Thank you for creating this feature request. We will investigate it and provide feedback as soon as we have some updates.

mahabaleshwars avatar Aug 30 '24 05:08 mahabaleshwars

Hi @nabheet šŸ‘‹, The setup-python action supports caching for a single package manager at a time, either pip or pipenv. To cache both pip and pipenv dependencies, you can use two separate steps to cache each package manager independently within the same workflow. Here's an example of how you can achieve this:

name: Cache pip and pipenv

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Cache pip packages
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Cache pipenv packages
        uses: actions/cache@v4
        with:
          path: ~/.local/share/virtualenvs
          key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-pipenv-

      - name: Install dependencies
        run: |
          pip install --upgrade pip
          pip install pipenv
          pipenv install --deploy --ignore-pipfile

In this example, theĀ actions/cacheĀ action is used to cache the pip dependencies located inĀ ~/.cache/pipĀ and the pipenv virtual environments located inĀ ~/.local/share/virtualenvs. Adjust the paths as necessary for your specific setup. The cache keys are generated based on the hash of theĀ requirements.txtĀ andĀ Pipfile.lockĀ files to ensure that the cache is updated when dependencies change.

I hope this helps! Let us know if you have any further questions.

priyagupta108 avatar Dec 31 '24 09:12 priyagupta108

Hi @nabheet,

Just a friendly reminder regarding the solution I provided for caching both pip and pipenv dependencies. Have you had a chance to try it out? If you have any questions or need further assistance, please let me know.

Thank you!

priyagupta108 avatar Jan 14 '25 12:01 priyagupta108

Sorry I just got back from vacation. I believe this should work for us. I will close this issue for now as it will take me a few days to confirm. Thank you!

nabheet avatar Jan 15 '25 13:01 nabheet