astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
19.39k stars 574 forks source link

Support a "flat" package layout in `uv init` #5328

Closed mgaitan closed 1 week ago

mgaitan commented 1 month ago

uv init mypackage currently generates a "src" layout:

.
├── pyproject.toml
├── README.md
└── src
    └── mypackage
        └── __init__.py

That's okay as the default because this layout has some known advantages.

However, it isn't a widely used convention, especially for simple, pure Python packages.

So I propose to allow creating a flat layout with uv init --flat:

.
├── pyproject.toml
├── README.md
└── mypackage
    └── __init__.py

Paraphrasing The Hitchhiker's Guide to Python:

Python allows many tricks, and some of them are potentially dangerous. [Python's] philosophy, very different from highly defensive languages like Java, which provide many mechanisms to prevent misuse, is expressed by the saying: “We are all responsible users.”

and (abusing) the Zen of Python:

Flat is better than nested.

zanieb commented 1 month ago

Thanks for the writeup!

I'm pretty hesitant, honestly. This results in confusing behavior for users due to the module search path including the package by default. I think we may be better off taking an opinionated stance here.

danielgafni commented 2 weeks ago

The lack of flat layout support makes migrating large monorepos (https://github.com/dagster-io/dagster) with dozens of packages much harder.

I agree with your point about the potential confusion with automatic package discovery when using the flat layourt, but there are already thousands of Python projects using it.

I think uv should prefer the src layout and use it by default, but at the same time adding support for flat layout seems reasonable to me. The opposite decision will slow down uv adoption.

zanieb commented 2 weeks ago

@danielgafni Sorry if I'm missing something here, but we just don't create a flat layout in uv init — as far as I know, uv itself should fully support flat layouts otherwise?

danielgafni commented 2 weeks ago

Hey, I first tried using the flat layout and got import errors for our packages.

For example, with the following pyproject.toml, running

uv pip install -e python_modules/dagster-pipes

passes with both flat and src layout, but only the src layout makes the package available for importing. I'm not sure what can cause this behavior. Moving dagster_pipes to /src and re-running uv pip install solves the problem, that's why I assumed flat layout wasn't supported.

danielgafni commented 2 weeks ago

Actually, I can't reproduce this issue anymore :open_mouth: Everything seems to be working, probably something was wrong on my side, sorry about that!

mgaitan commented 1 week ago

@zanieb I guess my original request have been implemented via uv init --app right? . We could close this.

charliermarsh commented 1 week ago

Correct! Thanks @mgaitan.