Modern Python Package Structure with pyproject.toml, uv, Scripts, and FastAPI

Python packaging has become much simpler with pyproject.toml as the modern project standard and uv as a fast, all-in-one frontend for dependency management, virtual environments, and execution.

This post shows a minimal, production-ready layout including:

  • a clean package structure
  • a runnable Python script
  • a FastAPI server
  • uv-based workflows (install, run, build)

myproject/
β”œβ”€ src/
β”‚  └─ myproject/
β”‚     β”œβ”€ __init__.py
β”‚     β”œβ”€ core.py
β”‚     β”œβ”€ script.py
β”‚     └─ api.py
β”œβ”€ tests/
β”‚  └─ test_core.py
β”œβ”€ pyproject.toml
└─ README.md

Two runnable components:

  • script.py β†’ a simple command-line script
  • api.py β†’ a minimal FastAPI server

βš™οΈ Minimal pyproject.toml

[project]
name = "myproject"
version = "0.1.0"
description = "A simple example Python package with scripts and a FastAPI server."
readme = "README.md"
requires-python = ">=3.9"
authors = [
  { name="Your Name", email="you@example.com" }
]

dependencies = [
  "fastapi",
  "uvicorn"
]

[project.optional-dependencies]
dev = ["pytest"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project.scripts]
myproject-script = "myproject.script:main"

[project.entry-points."uvicorn.run"]
myproject-api = "myproject.api:app"

πŸ“ Example Python Script

src/myproject/script.py

def main():
    print("Hello from my project script!")

if __name__ == "__main__":
    main()

Run it with uv

uv run myproject-script

Or run directly:

uv run src/myproject/script.py

πŸš€ Minimal FastAPI Server

src/myproject/api.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "Hello from FastAPI!"}

Create Python Virutal Environment

uv venv --seed .venv

Run the API server with uv

β€œbash


```bash
uv run uvicorn myproject.api:app --reload

Or, because we defined an entry point:

uv run uvicorn myproject-api

πŸ§ͺ Running Tests

uv run pytest

🧱 Installing Dependencies

uv pip install -e .[dev]

πŸ—οΈ Build & Publish

Build:

uv build

Publish:

uv publish

🎯 Summary

Using pyproject.toml + uv, you get:

  • A clean package structure
  • Fast script execution (uv run)
  • A minimal FastAPI server with entry points
  • Simple dependency + environment management
  • One tool for build & publish