What this thread does
Do first: build with python3 -m build, then install the resulting wheel into a clean environment and run its import and command from outside the source checkout.
Short answer
As of November 2026, this recipe uses Python 3.15.2 and a chosen Hatchling 1.33.0 backend pin. Declare the backend, package selection and command target explicitly.
Choose / don't choose
src/layout: choose to keep imports from accidentally resolving through the repository root; don't patchsys.pathto conceal a broken install (PyPA layout discussion).- Wheel install: choose for release acceptance; don't treat
pip install -e .as a substitute. Backend behavior can differ between editable and regular installations (pip). - Console entry point and module execution: choose one shared callable; don't maintain separate application implementations for
spam-cliandpython -m spam(entry-point specification, __main__).
Not for: choosing the project's environment manager; see python-venv-or-uv-and-make-my-agent-use-the-project-environment.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
build-system.build-backend | "hatchling.build" for Hatchling; match this to requires | Linked backend metadata guide | PyPA |
build-system.requires | Declare Hatchling as the build dependency; build requirements are separate from application dependencies | Chosen backend release | PyPA |
[project.scripts] | spam-cli = "spam:main_cli" means import spam, call main_cli() without arguments and use its result as the exit status | Current packaging metadata | guide, spec |
[tool.hatch.build.targets.wheel].packages | For a package named foo in src, ["src/foo"] installs it as foo, not src.foo; adapt to the real import package | Current Hatchling selection API | build configuration |
python -m pip install | Give the exact wheel path; run through the intended environment's interpreter | Regular installation | pip |
python -m spam | executes spam.__main__ | Current Python CLI | command line |
Order of operations
1. Put importable code in the package selected by the backend, with an ordinary __init__.py unless using intentional namespace packages. Set [project] name/version and runtime dependencies separately from backend requirements. (packaging tutorial). 2. Export the console callable from the module named by [project.scripts]. Keep __main__.py short: import that callable and invoke it through sys.exit(main_cli()). (__main__). 3. Install the build frontend with python3 -m pip install --upgrade build, then run python3 -m build. Inspect the wheel to ensure it contains the import package, its __main__.py, entry-point metadata and required data files (tutorial). 4. Create a separate virtual environment, invoke that environment's Python to install the exact wheel, and change to an unrelated empty directory before running the installed command and python -m form. Use a fresh environment for each artifact rather than leaving an editable install available (pip, layout). 5. In that clean environment and directory, run python -c "import importlib.util; print(importlib.util.find_spec('spam'))". None means no spec found; inspect a returned spec's origin, then perform the actual import and command invocation. A dotted name imports its parent during the check, so parent imports can themselves fail (importlib).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| “ModuleNotFound error” | Installed package cannot be imported in the reported example | Inspect the wheel contents and selected package path, then reinstall and check from an unrelated directory | jennirinker, March 26, 2025, Hatch selection |
| “ValueError: Unable to determine which files to ship inside the wheel” | No selection heuristic matches the intended package | Select wheel packages explicitly and inspect the layout; namespace packages need intentional configuration | drmason13, October 21, 2024, wheel reference |
Missing
- The linked source does not confirm: the proposed shared callable gives both invocation forms the same application and exit behavior.
- The linked source does not confirm: The distribution name may differ from the import name.
- The linked source does not confirm: the supplied example resolves spam on the module search path.
- The linked source does not confirm: An editable install can hide missing wheel files and working-directory assumptions.
- The linked source does not confirm: the illustrative build-system.requires=['hatchling==1.33.0'] pin in the PyPA guide; the linked Hatchling history confirms the release exists.
- The actual module tree and backend are unspecified;
spamandfooare separate documentation examples, not names to mix in one project. find_specestablishes discoverability, not successful execution of every import or runtime dependency. Resource files, compiled extensions and platform wheels need their own installed-artifact checks.
Started from a post by @Python Packaging Authority, packaged by the editor. Original post