Skip to content

Python: Allow module package names other than main #8535

Closed

Description

@helderco

Summary

Generate a src/my_module package for new modules instead of the hardcoded src/main package.

Motivation

The entrypoint for Python’s runtime container imports a module’s source code via an hardcoded import main:

def import_module(module_name: str = "main") -> types.ModuleType:
"""Import python module with given name."""
# TODO: Allow configuring which package/module to use.
try:
return importlib.import_module(module_name)

This forces the module’s layout to always be under src/main (unless otherwise configured for the build backend), so every module looks the same and forces extra configuration to customize something that should be as natural as what uv init or poetry init produces.

It’s also been a source of confusion for some users in how to properly break a bigger module into multiple files because they may not understand whether main refers to the package or the module, in a src/main/main.py file.

For example, in order to move the main object from __init__.py to a main.py file, some users try to import in __init__.py with:

import main

However, in a src layout, imports start from src, which means that the above file is actually importing itself!

It would have to be either:

import main.main  # absolute import: src/main/main.py

Or:

import .main  # relative import: ./main.py

The confusion over this double main has prevented me from changing the default template to put the source code under a src/main/main.py instead of src/main/__init__.py:

Note that the file main.py is aptly named because every Dagger module has a main object, so that’s what main refers to, in a multi-file package.

Specification

For a new Dagger module, generate a Python project that follows conventions more closely, perhaps actually delegating the project creation to uv init --lib:

  • Python project name following the Dagger module’s name, in kebab-case
  • Python package name for the source code following the same name, in snake_case

Additionally:

  • Example main object code under a main.py module
  • Dagger module description and Python import in __init__.py

And finally, for extra flexibility:

  • Introduce a Python entry point to enable customization over how the module’s code is imported

Example

For a new module:

❯ dagger init --sdk=python my-module

Generate the name my-module for the project:

[project]
-name = "main"
+name = "my-module"

And put sources under src/my_module:

.
 ├── sdk
 ├── src
-│  └── main
+│  └── my_module
 │     ├── __init__.py
+│     └── main.py
 ├── dagger.json
 ├── pyproject.toml
 └── uv.lock

With __init__.py with this import:

import .main

Or:

import my_module.main

Which should be clear now which “main” we’re trying to import here.

As for the entry point, we can default to:

[project.entry-points."dagger.mod"]
main_object = "my_module:MyModule"

But could just be in documentation and not necessarily generated in the initial template.

Rationale

This is something I’ve wanted to do since the very beginning:

# TODO: Allow configuring which package/module to use.

I presented this solution as a follow up to:

But have recently bumped the priority on this because of user confusion on correctly importing in __init__.py.

And there’s a reason for keeping the __init__.py in the default template instead of reverting to just src/main.py like it used to. It’s the most flexible, self documenting, and conventional default.

Without it, I’ve already seen users failing to break into multiple files correctly, but in any case, this is being re-added to the documentation in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions