[Python] Python custom src-layout: How to add another subpackage layer?

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have my Python package structured in src-layout. I run multiple github repositories with each the same super-package-name, structured as follows:

    package-subpackage1
    ├── .git/
    ├── src/
    │ └── package/
    │ └── subpackage1/
    │ ├── __init__.py
    │ └── code_subpackage1.py
    └── pyproject.toml


    package-subpackage2/
    ├── .git/
    ├── src/
    │ └── package/
    │ └── subpackage2/
    │ ├── __init__.py
    │ └── code_subpackage2.py
    └── pyproject.toml


    By setting the .toml in each repository to

    [tool.setuptools.packages.find]
    where = ["src"]
    include = ["package.*"]


    I can import the modules as

    import package.subpackage1

    import package.subpackage2


    Now I would like to add a subsubpackage-Layer to subpackage1, like

    package-subpackage1/
    ├── .git/
    ├── src/
    │ └── package/
    │ └── subpackage1/
    │ ├── __init__.py
    │ ├── code_subpackage1.py
    │ └── subsubpackage1/
    │ ├── __init__.py
    │ └── code_subsubpackage1.py
    └── pyproject.toml


    which I would like to be able to call via

    import package.subpackage1
    subpackage1.subsubpackage1.SomeClass


    When I try to access the subsubpackage1 via

    import package.subpackage1 as pack
    pack.subsubpackage1


    I get

    AttributeError: module 'package.subpackage1' has no attribute 'subsubpackage1'


    which is kind of expected, since I am breaking the vanilla src/-Layout and the automatic discovery seems to fail. However, if I manually import subsubpackage1 afterwards via

    import package.subpackage1.subsubpackage1


    all my classes in subsubpackage1.py become "magically" available via

    pack.subsubpackage1.SomeClass


    I don’t seem to understand how it is possible that a manual import of the subsubpackage1 makes it available in the previously imported subpackage1. Any idea why this is the case?

    And if I want to design my pyproject.toml correctly, do I have to explicitly set tools.packages.packages and reassemble my files into this custom structure as suggested in here ? Or is there a more standardized way to include another subpackage layer into the src-Layout?

    (Don’t be too hard on me pls, this if my first question ever on StackOverFlow :) )

    Continue reading...

Compartilhe esta Página