Declaring Python Scripts with `pyproject.toml`
Something I’m constantly forgetting is how to declare scripts with
pyproject.toml. This is a quick reference for me to come back to when I
inevitably forget this again.
Let’s work with basic project structure:
.
|-- pyproject.toml
`-- foo
|-- __init__.py
|-- __main__.py
`-- ...The file __main__.py will be the function we want to run in the script:
def main():
print("Hello from my app!")
if __name__ == "__main__":
main()Define the entry point under [project.scripts] in pyproject.toml:
[project]
name = "my-app"
version = "0.1.0"
[project.scripts]
foo = "foo.__main__:main" # Runs main() from foo/__main__.py
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["foo"]Now this can be executed with uv run foo if you’re using uv (which you
should be in this age!), or using pip install -e . then calling foo
Reply to this post by email blZake@proZbableodyssey.blog (remove Z characters) ↪