Skip to content

Indicate that python scripts need not be wholly placed in __init__.py #7430

Closed

Description

@esafak

What is the issue?

dagger's practice of using __init__.py to hold dagger scripts beyond simple imports flies in face of python convention. The documentation and templates should be revised to indicate that dagger does not in fact require or recommend this, and users are free to place their code in another file and import it in __init__.py so long as it is in a package called "main"

Discord discussion

Activity

  1. changed the title [-]Indicate that python scripts need not be wholly placed in __init__.py[/-] [+]Indicate that python scripts need not be wholly placed in `__init__.py`[/+] on May 21, 2024
  2. helderco commented on May 21, 2024

    @helderco
    Contributor

    To be clear, this is documented in Module Structure but perhaps it used to be clearer when the template created src/main.py by default.

    Noticing that people were confused about splitting that into multiple files (python module to package), I tried to simplify by defaulting to a package, but removed this part of the documentation because I overestimated how obvious it is:

    Multiple files

    You can split your Python module (main.py) into multiple files by turning it into a package:

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

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

    # 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

    - Docs excerpt removed in https://github.com/dagger/dagger/pull/7053/files#diff-a2b0f2ebe1418510e6e45065533f4ba5fef4d8cd819ae7c9a66e1b4961baa59bL54

    The current version of the docs already explain that dagger only needs for main to be importable, whether it's a python module or package.

    I'm not very fond of yet another main.py in:

    .
    └── src
       └── main
          ├── __init__.py
          └── main.py
    

    So I'm inclined to just leave a code comment in the template recommending people to create their own file names and just import on __init__.py.

  3. helderco commented on May 21, 2024

    @helderco
    Contributor

    Another alternative I'm considering is that it's planned since the beginning to allow importing "my_module" instead of "main". Then I'm perfectly fine with this structure:

    .
    └── src
       └── my_module       # name that dagger needs to know about (to import)
          ├── __init__.py  # for imports
          ├── main.py      # for main object
          └── test.py      # for Test object
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions