astral-sh / uv

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

Improve errors / diagnostics when package is missing `__init__.py` #8212

Open drmason13 opened 6 days ago

drmason13 commented 6 days ago

uv 0.4.18 Running uv build --package my-project in a project (within a workspace, though I suspect a more minimal repro is possible) where the workspace member package is laid out in a typical style:

pyproject.toml
src/
    my_project/
        < distinct lack of __init__.py >

I've seen this error a few times:

The most likely cause of this is that there is no directory that matches the name of your project (my_project)

but it can be a confusing and frustrating error when there is a directory with that name.

The actual problem is that python doesn't recognise the directory as a module/package due to the lack of __init__.py.

Forgetting/losing your __init__.py is a silly error to make but I think it's common enough that it is worth mentioning in uv's error that your directory also needs an __init__.py. You've done the hard part in naming the directory that is problematic.

I might suggest the following:

The most likely cause of this is that there is no directory that matches the name of your project (myproject) or that directory does not contain an __init_\.py file.

Before ``` ValueError: Unable to determine which files to ship inside the wheel using the following heuristics: https://hatch.pypa.io/latest/plugins/builder/wheel/#default-file-selection The most likely cause of this is that there is no directory that matches the name of your project (my_project). At least one file selection option must be defined in the `tool.hatch.build.targets.wheel` table, see: https://hatch.pypa.io/latest/config/build/ As an example, if you intend to ship a directory named `foo` that resides within a `src` directory located at the root of your project, you can define the following: [tool.hatch.build.targets.wheel] packages = ["src/foo"] ```
After ``` ValueError: Unable to determine which files to ship inside the wheel using the following heuristics: https://hatch.pypa.io/latest/plugins/builder/wheel/#default-file-selection The most likely cause of this is that there is no directory that matches the name of your project (my_project) or that directory does not contain an __init__.py file. At least one file selection option must be defined in the `tool.hatch.build.targets.wheel` table, see: https://hatch.pypa.io/latest/config/build/ As an example, if you intend to ship a directory named `foo` that resides within a `src` directory located at the root of your project, you can define the following: [tool.hatch.build.targets.wheel] packages = ["src/foo"] ```

Bonus points if uv actually checks and works out what is missing: __init__.py or the directory

charliermarsh commented 5 days ago

Unfortunately those errors come from the build backend, which we don't control. (In your case, it's coming from hatchling.) I think this is something we can improve once we've shipped our own build backend (see #3957), but I'm hesitant to add our own error handling heuristics around this stuff since different build backends may treat these cases differently, they may rely on build backend-specific configuration, etc.

drmason13 commented 5 days ago

Ah I see, thanks for the clarification. I can go ask the same question at the hatchling repo.

Absolutely fine to close this, I agree it's not your issue. Thank you for your excellent work with uv, it's been a joy to use!