|
| 1 | +# Python Template |
| 2 | + |
| 3 | +A basic Python project template that actually works. Nothing fancy, just the stuff you need to get going. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [What's in here](#whats-in-here) |
| 8 | +- [Getting started](#getting-started) |
| 9 | +- [Development](#development) |
| 10 | +- [Docker](#docker) |
| 11 | +- [Testing](#testing) |
| 12 | +- [Code quality](#code-quality) |
| 13 | +- [GitHub Actions](#github-actions) |
| 14 | +- [Project structure](#project-structure) |
| 15 | + |
| 16 | +## What's in here |
| 17 | + |
| 18 | +This template gives you a Python 3.12 project with: |
| 19 | + |
| 20 | +- **uv** for dependency management (it's fast) |
| 21 | +- **pytest** for testing |
| 22 | +- **ruff** for linting and formatting |
| 23 | +- **mypy** for type checking |
| 24 | +- **Docker** setup that actually builds |
| 25 | +- **GitHub Actions** for CI/CD |
| 26 | +- **Makefile** for common tasks |
| 27 | + |
| 28 | +## Getting started |
| 29 | + |
| 30 | +Clone this template and make it your own: |
| 31 | + |
| 32 | +```bash |
| 33 | +git clone <your-repo> |
| 34 | +cd <your-project> |
| 35 | +``` |
| 36 | + |
| 37 | +or just click the `Use this template` button. |
| 38 | + |
| 39 | +Install dependencies: |
| 40 | + |
| 41 | +```bash |
| 42 | +uv run make install |
| 43 | +``` |
| 44 | + |
| 45 | +That's it. You're ready to code. |
| 46 | + |
| 47 | +## Development |
| 48 | + |
| 49 | +The main code goes in `python_template/`. There's already a simple function in `main.py` to get you started. |
| 50 | + |
| 51 | +### Common commands |
| 52 | + |
| 53 | +```bash |
| 54 | +# Install everything |
| 55 | +uv run make install |
| 56 | + |
| 57 | +# Run tests |
| 58 | +uv run make test |
| 59 | + |
| 60 | +# Check your code (linting + type checking) |
| 61 | +uv run make check |
| 62 | + |
| 63 | +# Fix formatting issues |
| 64 | +uv run make fix |
| 65 | + |
| 66 | +# Run tests with coverage |
| 67 | +uv run make coverage |
| 68 | +``` |
| 69 | + |
| 70 | +### Adding dependencies |
| 71 | + |
| 72 | +Add them to `pyproject.toml` and run: |
| 73 | + |
| 74 | +```bash |
| 75 | +uv lock |
| 76 | +uv sync |
| 77 | +``` |
| 78 | + |
| 79 | +## Docker |
| 80 | + |
| 81 | +Build and run with Docker: |
| 82 | + |
| 83 | +```bash |
| 84 | +# Build the image |
| 85 | +docker build -t python-template . |
| 86 | + |
| 87 | +# Run it |
| 88 | +docker run -it python-template /bin/bash |
| 89 | + |
| 90 | +# Run tests in Docker |
| 91 | +docker run --rm python-template uv run make test |
| 92 | +``` |
| 93 | + |
| 94 | +## Testing |
| 95 | + |
| 96 | +Tests go in the `tests/` folder. The template includes a basic test to make sure everything works. |
| 97 | + |
| 98 | +Run tests: |
| 99 | + |
| 100 | +```bash |
| 101 | +uv run make test |
| 102 | +``` |
| 103 | + |
| 104 | +Get coverage: |
| 105 | + |
| 106 | +```bash |
| 107 | +uv run make coverage |
| 108 | +``` |
| 109 | + |
| 110 | +## Code quality |
| 111 | + |
| 112 | +The template enforces code quality with: |
| 113 | + |
| 114 | +- **ruff** for linting and formatting |
| 115 | +- **mypy** for type checking |
| 116 | +- **pre-commit** hooks (optional but recommended) |
| 117 | + |
| 118 | +Check everything: |
| 119 | + |
| 120 | +```bash |
| 121 | +uv run make check |
| 122 | +``` |
| 123 | + |
| 124 | +Fix formatting: |
| 125 | + |
| 126 | +```bash |
| 127 | +uv run make fix |
| 128 | +``` |
| 129 | + |
| 130 | +## GitHub Actions |
| 131 | + |
| 132 | +Three workflows are set up: |
| 133 | + |
| 134 | +- **CI**: Runs on every push/PR. Tests your code, checks formatting, runs security scans. |
| 135 | +- **Docker**: Builds and publishes Docker images to GitHub Container Registry. |
| 136 | +- **Release**: Creates releases when you push version tags. |
| 137 | + |
| 138 | +Just push your code and everything runs automatically. |
| 139 | + |
| 140 | +### Making a release |
| 141 | + |
| 142 | +```bash |
| 143 | +git tag v1.0.0 |
| 144 | +git push origin v1.0.0 |
| 145 | +``` |
| 146 | + |
| 147 | +This triggers the release workflow and publishes your Docker image. |
| 148 | + |
| 149 | +## Project structure |
| 150 | + |
| 151 | +``` |
| 152 | +python-template/ |
| 153 | +├── python_template/ # Your main code |
| 154 | +│ ├── __init__.py |
| 155 | +│ └── main.py |
| 156 | +├── tests/ # Your tests |
| 157 | +│ ├── __init__.py |
| 158 | +│ └── test_main.py |
| 159 | +├── .github/workflows/ # GitHub Actions |
| 160 | +├── Dockerfile # Docker setup |
| 161 | +├── Makefile # Common commands |
| 162 | +├── pyproject.toml # Project config and dependencies |
| 163 | +├── requirements.txt # Frozen dependencies |
| 164 | +└── README.md # This file |
| 165 | +``` |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +That's pretty much it. This template handles the boring setup stuff so you can focus on writing code. Change the name in `pyproject.toml`, update this README, and start building. |
0 commit comments