3

I'm trying to cache the python dependencies of my project. To do that, I have this configuration in my workflow:

      - uses: actions/cache@v2
        id: cache
        with:
          path: ~/.cache/pip
          key: pip-${{ runner.os }}-${{ hashFiles('**/requirements.txt') }}-${{ hashFiles('**/requirements_dev.txt') }}
          restore-keys: pip-${{ runner.os }}

      - name: Install apt dependencies
        run: |
          sudo apt-get update
          sudo apt-get install gdal-bin

      - name: Install dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          pip install --upgrade pip==9.0.1
          pip install -r requirements.txt
          pip install -r requirements_dev.txt

This works, by 'works' I mean that it loads the cache and skip the 'Install dependencies step' and it restores the ~/.cache/pip directory. The problem is that when I try to run the tests, the following error appears:

  File "manage.py", line 7, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Error: Process completed with exit code 1.

Am I caching the incorrect directory? Or what am I doing wrong?

Note: this project is using python2.7 on Ubuntu 16.04

Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33
  • As the workflow defiintion you posted is not complete: are you running everything in the same job? – riQQ Jan 10 '21 at 16:43
  • I think you still have to run pip install. The difference is that pip will use the cache instead of redownloading the packages. After installing, the packages are stored in another folder, see https://stackoverflow.com/questions/29980798/where-does-pip-install-its-packages – riQQ Jan 10 '21 at 17:11
  • And there is not a way to cache dependencies like in node with the node_modules folder? – Antonio Gamiz Delgado Jan 10 '21 at 17:23

1 Answers1

1

As it explains here, you can cache the whole virtual environment:

- uses: actions/cache@v2
  with:
    path: ${{ env.pythonLocation }}
    key: ${{ env.pythonLocation }}-${{ hashFiles('setup.py') }}-${{ hashFiles('dev-requirements.txt') }}

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33