TIL: Python Template Project

1. Initialize project as a uv packaged application

uv init --package project-name

2. Create venv by running it

cd project-name
uv run project-name

3. Add dependencies to pyproject.toml and install them

uv add fire joblib loguru tqdm

4. Add pre-commit hooks

Create a .pre-commit-config.yaml file in the root of your project with the following content:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: check-added-large-files
        args: ['--maxkb=1000']
      - id: check-ast
      - id: check-builtin-literals
      - id: check-json
      - id: check-merge-conflict
      - id: check-toml
      - id: check-yaml
      - id: detect-private-key
      - id: end-of-file-fixer
      - id: mixed-line-ending
        args: ['--fix=lf']
      - id: pretty-format-json
        args: ['--autofix']
      - id: trailing-whitespace
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.14.5
    hooks:
      - id: ruff-check
        args: [--fix]
      - id: ruff-format

Install them by running:

uv add --dev pre-commit
uv run pre-commit install

Run them on all files:

git add .
uv run pre-commit run --all-files
git commit -m "Initial commit"

Add pre-commit GitHub Action by creating a file .github/workflows/pre-commit.yml with the following content:

name: pre-commit

on:
  pull_request:
  push:
    branches: [main]

jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: pre-commit/action@v3.0.1



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • TIL: Multi-node GPU training with SkyPilot and PyTorch Lightning
  • TIL: Request GCP quota increase
  • TIL: Useful Datamol functions
  • TIL: Template data processing script with pathlib, fire, joblib, loguru, and tqdm
  • TIL: Template data exploration Jupyter notebook