Search code examples
pythonpython-packagingpython-poetry

Poetry: build a package with a non-standard directory structure


I created a repo with this non-standard structure:

- src
 - resources
  -- a.py
  -- b.py ...

I want to make a package out of it (let's call it pack).

In my pyproject.toml, the relevant lines are:

[tool.poetry]
name = "pack"
include=[{include="src"}]

Then after a pip install everything is installed under src. Using it would mean from src import ... not from pack import ....

If I follow the standard tree (everything under src/pack), then it works as expected.

Question

Is there a way, with my tree, to have the package built so as from pack import ... works?


Solution

  • If you want to use pack in your import statements, you need a folder that is called like this. This is how python discovers a package.

    Where it is located and how your project is named is a different story. If you have a project structure like this:

    pack
    ├── README.md
    ├── pyproject.toml
    └── src
        └── pack
            ├── __init__.py
            ├── a.py
            └── b.py
    

    you must have this in your pyproject.toml

    [tool.poetry]
    name = "pack"
    packages = [{include = "pack", from = "src"}]