Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/current_docs/guides/457482-create-app-ci-module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This will generate a `dagger.json` module file, an initial `dagger/main.go` sour
dagger init --name=my-module --sdk=python
```

This will generate a `dagger.json` module file, initial `dagger/src/main.py` and `dagger/pyproject.toml` files, as well as a generated `dagger/sdk` folder for local development.
This will generate a `dagger.json` module file, initial `dagger/src/main/__init__.py`, `dagger/pyproject.toml` and `dagger/requirements.lock` files, as well as a generated `dagger/sdk` folder for local development.

</TabItem>
<TabItem value="TypeScript">
Expand Down Expand Up @@ -106,7 +106,7 @@ This function does the following:
</TabItem>
<TabItem value="Python">

Next, update the generated `dagger/src/main.py` file with the following code:
Next, update the generated `dagger/src/main/__init__.py` file with the following code:

```python file=./snippets/create-app-ci-module/base/python/main.py
```
Expand Down
2 changes: 1 addition & 1 deletion docs/current_docs/guides/620942-github-google-cloud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The next step is to create a Dagger Module to do the heavy lifting: build a cont
dagger install github.com/vvaswani/daggerverse/google-cloud-run
```

1. Update the generated `dagger/src/main.py` file with the following code.
1. Update the generated `dagger/src/main/__init__.py` file with the following code.

```python file=./snippets/github-google-cloud/python/main.py
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,48 @@ Of course, for tools that support it, you can simply use the CLI, as below:
poetry add infisical-python
```

## Pinned dependencies
## Lock file

Since the module is installed via [`pip install .`](./820256-module-structure.mdx#modules-as-python-libraries), lock files aren't supported yet because they're not a part of the standard build system and thus require using the tool directly. In the meantime, you'll need to pin it:
While Python doesn't have a [lock file standard](https://discuss.python.org/t/lock-files-again-but-this-time-w-sdists/46593/313) yet, other than using a tool that has a proprietary format (like Poetry), the most common way to do it is with a `pip`-compatible format like [`pip-tools`](https://pip-tools.readthedocs.io/en/latest/) uses.

```toml
[project]
dependencies = [
"infisical-python==2.1.8"
]
```

With Hatch, there is also the option to use a [plugin](https://pypi.org/project/hatch-requirements-txt/) to dynamically use a `requirements.txt` file in `dependencies`, which allows for a [`pip-tools`](https://pypi.org/project/pip-tools/) workflow:
Since the introduction of [uv](./820256-module-structure.mdx#uv-installer), dependencies are pinned by default using a `requirements.lock` file. If it exists, the Python SDK will use it directly to install dependencies, resulting in a faster installation.

```toml
[build-system]
requires = ["hatchling", "hatch-requirements-txt"]
build-backend = "hatchling.build"
:::tip
Check [compatibility with `pip` and `pip-tools`](https://github.com/astral-sh/uv/blob/main/PIP_COMPATIBILITY.md) if migrating from or integrating with those tools.
:::

[project]
name = "my-module"
version = "0.1.0"
dynamic = ["dependencies"]
The lock file is only created automatically for new modules. For existing modules that don't have it, it can be created (and updated) manually. For example:

[tool.hatch.metadata.hooks.requirements_txt]
files = ["requirements.txt"]
```console
uv pip compile --generate-hashes --update-all -o requirements.lock pyproject.toml sdk/pyproject.toml
```

Using the fast [`uv`](https://github.com/astral-sh/uv) tool, you can now manage constraints in a separate file:
The above command pulls in dependencies from `pyproject.toml` and `sdk/pyproject.toml`, gets their latest compatible versions, and writes them to `requirements.lock` with added hashes to verify integrity when downloading during install.

:::important
Make sure that `./sdk/pyproject.toml` is up to date first with:

```console
dagger develop
```
# requirements.in
infisical-python>=2.1.6
```
:::

Then, generate a pinned file from those constraints:
:::note
The lock file should only include dependencies, and not the editable installs for the `./sdk` and current module.
:::

```
uv pip compile requirements.in -o requirements.txt
```
### Poetry's lock file

The `requirements.txt` file now contains:
Dagger doesn't support running the `poetry` CLI. It depends on standards like [PEP 517](./820256-module-structure.mdx#modules-as-python-libraries). That means that it doesn't know about the `poetry.lock` file, but the `requirements.lock` file can be generated with [`poetry export`](https://python-poetry.org/docs/cli#export):

```console
poetry export --with dev --without main -o requirements.lock
```
# This file was autogenerated by uv v0.1.5 via the following command:
# uv pip compile requirements.in -o requirements.txt
infisical-python==2.1.8

:::important
The `dagger-io` dev dependency is [not exported correctly](https://github.com/python-poetry/poetry-plugin-export/issues/145), so it needs to be removed:

```console
sed -i '' '/dagger-io @/d' requirements.lock
```
:::
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Create a new directory on your filesystem and run `dagger init` to bootstrap you
cd potato
```

This will generate a `dagger.json` module file, initial `dagger/src/main.py` and `dagger/pyproject.toml` files, as well as a generated `dagger/sdk` folder for local development.
This will generate a `dagger.json` module file, initial `dagger/src/main/__init__.py`, `dagger/pyproject.toml` and `dagger/requirements.lock` files, as well as a generated `dagger/sdk` folder for local development.

The auto-generated Dagger Module template comes with some example Dagger Functions. Test it with the `dagger call` command:

Expand All @@ -56,7 +56,7 @@ When using `dagger call`, all names (functions, arguments, struct fields, etc) a

Dagger Modules contain one or more Dagger Functions. You saw one of them in the previous section, but you can also add your own.

Replace the auto-generated examples in `dagger/src/main.py` with your own, simpler Dagger Function:
Replace the auto-generated examples in `dagger/src/main/__init__.py` with your own, simpler Dagger Function:

```python file=./snippets/first-module/step2/main.py
```
Expand Down Expand Up @@ -130,7 +130,7 @@ Now, put everything you've learnt to the test, by building a Dagger Module and D
cd trivy
```

1. Replace the generated `dagger/src/main.py` file with the following code:
1. Replace the generated `dagger/src/main/__init__.py` file with the following code:

```python file=./snippets/first-module/trivy/main.py
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ The following code snippet shows how to use Python's
```python file=./snippets/documentation/docstrings/main.py
```

:::note
To use multiple Python source files, the `src/main.py` module needs to be converted
into a [package][pypkg]: `src/main/__init__.py`. In this case, the Dagger Module
is documented at the top of the `__init__.py` file, just like any Python package.
:::

## Function arguments

For function arguments, [annotate][typing-doc] with the [`dagger.Doc`][dag-doc] metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,82 @@ toc_max_heading_level: 2
title: "Module Structure"
---


# Module Structure


It's essential to understand a few key concepts about Dagger Modules created for use with the Python SDK, for a better fit with your normal development workflow.

## Runtime container with Python 3.11
## Python version

Dagger Modules run in a [container](../overview/942201-execution-environment.mdx) that's bootstrapped specifically by the Python SDK, with the necessary environment to run the module's code. It currently runs Python 3.11 by default but, starting with [Dagger v0.11.0][v11], the version can be changed.

The desired interpreter version can be pinned by creating a `.python-version` file next to your `pyproject.toml`:

```console
Comment thread
helderco marked this conversation as resolved.
Resolved
echo "3.11" > .python-version
```

This can be useful either to pin the version in case the default one gets an upgrade, or to choose a newer version.

As an alternative, setting [`requires-python`](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#python-requires) in `pyproject.toml` will also pin the version, but using a lower bound constraint (minimum version). For example, the following will use Python 3.11, even though Python 3.12 is available:

```toml
[project]
requires-python = ">=3.11"
```

## Base image

The default base image is [python:VERSION-slim](https://hub.docker.com/_/python/tags?name=3.11-slim) (based on the [Python version](#python-version)), but it can be overridden in `pyproject.toml` since [Dagger v0.11][v11]:

```toml
[dagger.tool]
base-image = "acme/python:3.11"
```

This can be useful to add a few requirements to the module's execution environment, such as system packages like `git`.

Dagger Modules run in a container (called the "runtime container") that's bootstrapped specifically by the Python SDK, with the necessary environment to run the module's code. It's currently hardcoded to run in Python 3.11 (although this may be configurable in future).
:::warning
Don't deviate from the default base image too much or it may break in a future version of Dagger. Override at own risk.
:::

## Automatic SDK install

The Python SDK is installed automatically, including dependencies, with a version that's tied to the currently running Dagger Engine container[^1]:
Each release of Dagger bundles the Python SDK inside the Engine container image. During the Dagger module's code generation phase, that directory is unpacked and installed automatically, including dependencies. That directory is the same one that is exported to the host as `./sdk` when running `dagger init --sdk=python` or `dagger develop --sdk=python`. Note that it is **not** the same as the directory obtained when installing from [dagger-io](https://pypi.org/project/dagger-io/).

The runtime container installs the SDK as an editable install, similar to the following:

```shell
# executed by the runtime container
pip install -e /sdk
uv pip install -e ./sdk
```

This is why the initial `pyproject.toml` file doesn't include any [dependencies](./077133-python-dependencies.mdx).

[^1]: The SDK files are mounted under `/sdk` in the Dagger Engine runtime container.
### UV installer

[uv](https://github.com/astral-sh/uv) is used internally to create the virtual environment, install dependencies and create the [lock file](./077133-python-dependencies.mdx#lock-file) (starting with [Dagger v0.11.0][v11]). It's possible to specify a newer version, if needed, in `pyproject.toml`:

```toml
[tool.dagger]
uv-version = "0.1.30"
```

If `pip` is required instead, `uv` can be disabled:

```toml
[tool.dagger]
use-uv = false
```

Note, however, that [pinning](./077133-python-dependencies.mdx#lock-file) via `requirements.lock` is only supported with `uv`.

## Modules as Python libraries

Dagger Modules in Python are built to be installed, like libraries. The runtime container installs the module code with:

```shell
# executed by the runtime container
pip install .
# executed by the runtime
uv pip install -e .
```

Due to [PEP 517](https://peps.python.org/pep-0517/) and [`pip` supporting `pyproject.toml`](https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/), this means that all package managers that support the [build backend](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#declaring-the-build-backend) standard are automatically supported.
Expand All @@ -47,30 +93,10 @@ Read more about this in the [Packaging Python Projects](https://packaging.python

The module's code isn't executed directly like a script, but it is rather imported under the hardcoded name `main` (although this may be configurable in future).

This means that after `pip install .`, there should be a Python module (or package) called `main` in the virtual environment's `site-packages/` directory inside the runtime container (`/opt/venv/lib/python-3.11/site-packages/main`).
This means that after `uv pip install -e .`, there should be a Python module (or package) called `main` in the virtual environment's `site-packages/` directory inside the runtime container (`/opt/venv/lib/python-3.11/site-packages/main` by default).

This is the most important requirement. The other conventions are flexible, as long as the above is verified.

## Multiple files

You can split your Python module (`main.py`) into multiple files by turning it into a [package](https://realpython.com/python-modules-packages/):

```shell
mkdir src/main
mv src/main.py src/main/__init__.py
```

Ensure that Dagger sees it by importing it in `__init__.py`:

```python
# src/main/__init__.py
import dagger
from dagger import dag, function

from .test import Test # in src/main/test.py
from .lint import Lint # in src/main/lint.py
```

## Project name

The default project name in `pyproject.toml` is also `main`:
Expand All @@ -87,14 +113,14 @@ Here is an example of using a different project name with Poetry (truncated for
```toml
[tool.poetry]
name = "awesome-module"
packages = [{ include = "main.py", from = "src" }]
packages = [{ include = "main", from = "src" }]
```

## Default project layout

The default template from `dagger init` creates a `main.py` file inside a `src` directory. This follows a [Python convention](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) that requires a project to be installed in order to run its code, which prevents accidental usage of development code since the Python interpreter includes the current working directory as the first item on the import path (more information is available in this [blog post on Python packaging](https://blog.ionelmc.ro/2014/05/25/python-packaging/)).
The default template from `dagger init` creates a `main` package inside a `src` directory. This follows a [Python convention](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) that requires a project to be installed in order to run its code. This prevents accidental usage of development code since the Python interpreter includes the current working directory as the first item on the import path (more information is available in this [blog post on Python packaging](https://blog.ionelmc.ro/2014/05/25/python-packaging/)).

Tools like [build](https://build.pypa.io/en/latest/) (default), [Hatch](https://hatch.pypa.io/latest/), and [Poetry](https://python-poetry.org/docs/) already detect this project layout by default, but it's not required and can be overridden as long as `main` is installed correctly in `site-packages`, or is at least importable with `import main`.
Tools like [build](https://build.pypa.io/en/latest/), [Hatch](https://hatch.pypa.io/latest/), and [Poetry](https://python-poetry.org/docs/) already detect this project layout by default, but it's not required and can be overridden as long as `main` is installed correctly in `site-packages`, or is at least importable with `import main`.

Here is an example of moving the `main` package next to `pyproject.toml`, resulting in the following structure:

Expand All @@ -105,13 +131,78 @@ Here is an example of moving the `main` package next to `pyproject.toml`, result
│ ├── lint.py
│ └── test.py
├── dagger.json
└── pyproject.toml
├── pyproject.toml
└── requirements.lock
```

In this case, it is necessary to to explicitly tell Poetry where to find the package, as shown below (truncated for brevity):
In this case, it is necessary to explicitly tell Poetry where to find the package, as shown below (truncated for brevity):

```toml
[tool.poetry]
name = "awesome-module"
packages = [{ include = "main" }]
```

## Alternative project template

If a different project layout is needed, `dagger init` won't override existing `pyproject.toml` and `.py` files. For example, to create the initial files with [Rye](https://rye-up.com):


```console
rye init --name=main acme
cd acme
dagger init --sdk=python --source=.
```

Rye will create an initial `src/main/__init__.py` but Dagger won't recognize any Dagger Functions in it. Either remove it and let Dagger create the default one, or adapt it to your needs before running `dagger call`.

Comment thread
helderco marked this conversation as resolved.
Resolved

## Poetry support

[Poetry](https://python-poetry.org) is a very popular workflow tool for Python. It doesn't fully support the [PEP 621](https://peps.python.org/pep-0621/) yet, but [it's coming](https://github.com/python-poetry/poetry/pull/9135).

It does support [PEP 517](https://peps.python.org/pep-0517/) however, which makes it possible to use with Dagger. Same as in [alternative project template](#alternative-project-template), the project can be initialized with:

```console
mkdir acme
cd acme
poetry init --name main --python "^3.11"
```

Remove the `"readme"` entry in `pyproject.toml` so Dagger doesn't fail with the unknown file. Here's an example of a minimal Poetry configuration:

```toml
[tool.poetry]
name = "main"
version = "0.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = ">=3.11"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
```

After `dagger init --sdk=python`, the `./sdk` can be added as a development dependency:

```console
poetry add -e --group=dev ./sdk
```

Which adds the following to `pyproject.toml`:

```toml
[tool.poetry.group.dev.dependencies]
dagger-io = {path = "sdk", develop = true}
```


## Other workflow tools

Dagger is agnostic to the tool used [in development](./742020-ide-integration.mdx). Similarly to Rye and Poetry, as long as the right standards are supported, Dagger should be able to use it too.


[v11]: https://github.com/dagger/dagger/releases/tag/sdk%2Fpython%2Fv0.11.0
4 changes: 2 additions & 2 deletions docs/current_docs/quickstart/428201-custom-function.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This will generate a `dagger.json` module file, an initial `dagger/main.go` sour
dagger develop --sdk=python
```

This will generate a `dagger.json` module file, initial `dagger/src/main.py` and `dagger/pyproject.toml` files, as well as a generated `dagger/sdk` folder for local development.
This will generate a `dagger.json` module file, initial `dagger/src/main/__init__.py`, `dagger/pyproject.toml` and `dagger/requirements.lock` files, as well as a generated `dagger/sdk` folder for local development.

</TabItem>
<TabItem value="TypeScript">
Expand Down Expand Up @@ -93,7 +93,7 @@ This new `BuildAndPublish()` function accepts two arguments - the source directo
</TabItem>
<TabItem value="Python">

Update `dagger/src/main.py` with the following code:
Update `dagger/src/main/__init__.py` with the following code:

```python file=./snippets/create/python/main.py
```
Expand Down