uv
3f9cfbb5 - Add experimental direct src layout

Commit
130 days ago
Add experimental direct src layout Currently, there are two main options for a project layout, as described in https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/. `<package_name>/__init__.py`: The project is importable even when not installed. The community seems to be migrating to the `src` layout https://hynek.me/articles/testing-packaging/. `src/<package_name>/__init__.py`: We add an extra layer of directory only to repeat the package name. This makes the library layout feel higher overhead than needed, making adoption especially in small projects harder, where the alternative is just having some Python files in a directory without any packaging. As an alternative, we introduce `src/__init__.py`, a layout familiar from other programming languages. It is low overhead (no extra indirection, docs are "put your python files in `src`, `uv sync` and `import <package_name>`") and it enforces isolation through (editable) installation (you can't `import <package_name>` directly). Python does not support this natively it always want the module name in the directory or filename. We hack around this by using a custom meta finder that we insert at the top import priority. This code runs at every python startup. Imports are lazy where possible to reduce overhead. Since we're hacking the Python import system to achieve this, it's experimental. I like `src/__init__.py` a lot, but everything that runs code through `.pth` in implicit, pre-main life is to be regarded with some suspicion. This PR only adds editable support (the harder part), proper `build_{sdist,wheel}` will be added in an upstack PR.
Author
Parents
  • File
    Cargo.lock
  • crates
    • uv-build-backend
      • File
        Cargo.toml
      • src
        • File
          lib.rs
        • File
          metadata.rs
        • File
          uv_direct_src_finder.py
        • File
          wheel.rs
    • uv-dispatch/src
      • File
        lib.rs
    • uv
      • src
        • commands
          • File
            build_backend.rs
        • File
          lib.rs
      • tests/it
        • File
          sync.rs