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)
π Recommended Project Layout
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 scriptapi.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
You might also like
Modern Python Package Structure with pyproject.toml, uv, Scripts, and FastAPI
A concise guide to building a clean, modern Python project using pyproject.toml, uv, scripts, and FastAPI.
Managing Multiple GPG Keys and YubiKey Setup
A practical guide to managing multiple GPG private keys β exporting, importing, backing up, and securely storing them on a YubiKey for signing, encryption, and SSH authentication.
Inside MCP: Develop your first MCP Server
A step-by-step guide for developers to build and test MCP servers that connect agents with real systems.