Skip to main content

A client package for running Dagger pipelines in Python.

Project description

Dagger Python SDK

PyPI Version Conda Version Supported Python Versions License Code style Ruff

A client package for running Dagger pipelines.

What is the Dagger Python SDK?

The Dagger Python SDK contains everything you need to develop CI/CD pipelines in Python, and run them on any OCI-compatible container runtime.

Requirements

  • Python 3.10 or later
  • Docker, or another OCI-compatible container runtime

A compatible version of the Dagger CLI is automatically downloaded and run by the SDK for you, although it’s possible to manage it manually.

Installation

From PyPI, using pip:

pip install dagger-io

You can also install via Conda, from the conda-forge channel:

conda install dagger-io

Example

Create a main.py file:

import sys

import anyio
import dagger


async def main(args: list[str]):
    async with dagger.Connection() as client:
        # build container with cowsay entrypoint
        ctr = (
            client.container()
            .from_("python:alpine")
            .with_exec(["pip", "install", "cowsay"])
            .with_entrypoint(["cowsay"])
        )

        # run cowsay with requested message
        result = await ctr.with_exec(args).stdout()

    print(result)


anyio.run(main, sys.argv[1:])

Run with:

$ python main.py "Simple is better than complex"
  _____________________________
| Simple is better than complex |
  =============================
                             \
                              \
                                ^__^
                                (oo)\_______
                                (__)\       )\/\
                                    ||----w |
                                    ||     ||

Note It may take a while for it to finish, especially on first run with cold cache.

If you need to debug, you can stream the logs from the engine with the log_output config:

config = dagger.Config(log_output=sys.stderr)
async with dagger.Connection(config) as client:
    ...

Learn more

Development

This library is maintained with Poetry.

If you already have a Python 3.10 or later interpreter in your $PATH, you can let Poetry manage the virtual environment automatically. Otherwise you need to activate it first, before installing dependencies:

poetry install

The following commands are available:

  • poe test: Run tests.
  • poe fmt: Re-format code following common styling conventions.
  • poe lint: Check for linting violations.
  • poe generate: Regenerate API client after changes to the codegen.
  • poe docs: Build reference docs locally.

Engine changes

Testing and regenerating the client may fail if there’s changes in the engine code that haven’t been released yet.

The simplest way to run those commands locally with the most updated engine version is to build it using Dagger’s CI pipelines :

../../hack/make sdk:python:test
../../hack/make sdk:python:generate

You can also build the CLI and use it directly within the Python SDK:

../../hack/dev poe test

Or build it separately and tell the SDK to use it directly (or any other CLI binary):

../../hack/make
_EXPERIMENTAL_DAGGER_CLI_BIN=../../bin/dagger poe test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dagger_io-0.6.1.tar.gz (53.9 kB view hashes)

Uploaded Source

Built Distribution

dagger_io-0.6.1-py3-none-any.whl (57.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page