pythonrequirements.txtpre-build-eventgitpod

Setting up requirements.txt in prebuild gitpod.yml


Whenever I open my gitpod workspace I have to re-install my requirements.txt file. I was reading about the gitpod.yml file and see that I have to add it in there so the dependencies get installed during the prebuild.

I can't find any examples of this so I just want to see if I understand it correctly.

Right now my gitpod.yml file looks like this...

    image:
       file: .gitpod.Dockerfile

    # List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
    tasks:
      - init: echo 'init script' # runs during prebuild
        command: echo 'start script'
    
    # List the ports to expose. Learn more https://www.gitpod.io/docs/config-ports/
    ports:
      - port: 3000
        onOpen: open-preview

    vscode:
      extensions:
        - ms-python.python
        - ms-azuretools.vscode-docker
        - eamodio.gitlens
        - batisteo.vscode-django
        - formulahendry.auto-close-tag
        - esbenp.prettier-vscode

Do I just add these two new 'init' and 'command' lines under tasks?

    image:
      file: .gitpod.Dockerfile

    # List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
    tasks:
      - init: echo 'init script' # runs during prebuild
        command: echo 'start script'
      - init: pip3 install -r requirements.txt
        command: python3 manage.py

    # List the ports to expose. Learn more https://www.gitpod.io/docs/config-ports/
    ports:
      - port: 3000
        onOpen: open-preview

    vscode:
      extensions:
        - ms-python.python
        - ms-azuretools.vscode-docker
        - eamodio.gitlens
        - batisteo.vscode-django
        - formulahendry.auto-close-tag
        - esbenp.prettier-vscode

Thanks so much for your help. I'm still semi-new to all this and trying to figure my way around.


Solution

  • To install requirements in the prebuild, you have to install them in the Dockerfile. The exception is editable installs, pip install -e ..

    For example, to install a package named <package-name>, add this line to .gitpod.Dockerfile:

    RUN python3 -m pip install <package-name>
    

    Installing from a requirements file is slightly trickier because the Dockerfile can't "see" the file when it's building. One workaround is to give the Dockerfile the URL of the requirements file in the repo.

    RUN python3 -m pip install -r https://gitlab.com/<gitlab-username>/<repo-name>/-/raw/master/requirements.txt
    

    Edit: Witness my embarrassing struggle with the same issue today: https://github.com/gitpod-io/gitpod/issues/7306