Skip to content

deargen/python-project-template-2024

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

python-project-template-2024

Ruff Actions status
Ruff Actions status
pytest doctest Actions status
uv Actions status
Built with Material for MkDocs Actions status

We use all good stuffs like:

ruff, uv, basedpyright, pytest, doctest, MkDocs, versioneer, GitHub Actions, conventional commit, changelog, typer CLI, rich logging, ...

This template is designed to make it easy to install in various ways as follows.

# For developers
pip install -e .

# If you want to use both API and CLI
pip install .
pip install 'git+https://github.com/deargen/python-project-template-2024'

# If you want to use only CLI
pipx install .
pipx install 'git+https://github.com/deargen/python-project-template-2024'
uv tool install .
uv tool install 'git+https://github.com/deargen/python-project-template-2024'

Key features

  1. Use GitHub Actions to check ruff formatter, ruff linter, pytest (unit test) pass or not
    • Actions can apply formatting
  2. Use GitHub Actions to check uv pip-compile is applied or not (Convert dynamic version in deps/requirements.in to lock file in deps/lock)
    • Actions can compile lock file
  3. Use MkDocs to automatically generate documents
    • Use GitHub Pages for public repo
    • Use GitLab Pages for private repo
  4. Versioning the project and automatically generating changelog.
  5. Provide convenience features with CLI (typer)
    • ml-project health to check environment settings.
    • ml-project --version to check the current version.

πŸƒ Try it out

  1. (Optional) pip3 install --user uv or brew install uv (macOS) to install uv, to enable faster uv pip over pip.
  2. uv pip install -r deps/lock/x86_64-manylinux_2_28/requirements.txt, uv pip install -e . to install dependencies and the package "ml-project"
  3. ml-project config to copy template.env -> .env. (Modify if necessary)
  4. ml-project health to check health of the environment and installation.
  5. python tools/examples/color_logging_main.py: example script with rich logging. Logs are saved in data/logs folder.
    • ML_PROJECT_LOG_LEVEL=WARNING python tools/examples/color_logging_main.py to exclude INFO logs.
  6. uv pip install -r deps/lock/x86_64-manylinux_2_28/requirements_dev.txt to install pytest and other developer packages.
  7. pytest to run tests.
    • doctest is automatically run in Actions.
  8. import ml_project; print(ml_project.__version__) to see the version like 0.1.0+4.g75bbed7.dirty.
    • It means 4 commits after 0.1.0 version. And if there are uncommitted changes, it is a dirty version.

πŸ“° File descriptions

πŸ“‚ .vscode/
β”‚ πŸ“„ settings.json          # VSCode settings for ruff auto formatting on save etc.
β”” πŸ“„ extensions.json        # List of extensions to recommend in VSCode.

πŸ“‚ .github/
β”‚ πŸ“‚ ISSUE_TEMPLATE/
β”” πŸ“‚ workflows/             # GitHub Actions for automated deployment pipeline
  β”” πŸ“„ *.yml

πŸ“‚ src/
β”” πŸ“‚ ml_project/            # `import ml_project` to define functions, classes, etc.
  β”‚ 🐍 __init__.py
  β”‚ 🐍 _version.py          # versioneer file to read version information from git tag (DO NOT modify)
  β”‚ πŸ“œ template.env         # Environment settings file copied to `.env` when `ml-project config` is executed
  β”” 🐍 ...

πŸ“‚ tools/                   # Files that can be run directly without importing. (e.g. train.py)

πŸ“‚ tests/                   # Functions executed when `pytest` is run

πŸ“‚ scripts/                 # Not directly related to the project, but necessary for project management
β”‚ # Script to find modules when generating MkDocs reference page.
β”‚ # It is possible to modify if there are modules you want to exclude manually!
β”” 🐍 gen_ref_nav.py

πŸ“‚ deps/
β”‚ # DO NOT modify. Created when `.github/workflows/apply-pip-compile.yml` is executed
β”‚ πŸ›‘οΈ .requirements.in.sha256
β”‚ πŸ›‘οΈ .requirements_dev.in.sha256
β”‚ πŸ›‘οΈ .requirements_docs.in.sha256
β”‚
β”‚ # Dependencies required for program users.
β”‚ πŸ–ŠοΈ requirements.in
β”‚ # Dependencies required for developers, not program users.
β”‚ πŸ–ŠοΈ requirements_dev.in
β”‚ # Programs required for document generation with mkdocs
β”‚ πŸ–ŠοΈ requirements_docs.in
β”‚ # NOTE: *.in files are package dependencies, not lock files, so they should all be written in dynamic version.
β”‚
β”‚ # DO NOT modify. Created from *.in files
β”” πŸ“‚ lock/
  β”‚ πŸ”’ requirements.txt
  β”‚ πŸ”’ requirements_dev.txt
  β”” πŸ”’ requirements_docs.txt

βš™οΈ pyproject.toml            # Python project integration information. Including settings for external tools like ruff.

πŸ›• How to use this template

To change the project name, modify the contents of ./replace_project_name.sh and run it. The script will take care of the following.

  1. Change the src/ml_project folder name to the desired name (import ml_project name)
  2. Modify the parts to be changed in pyproject.toml. (Commented)
  3. Change the badge URLs in README.md (python-project-template-2024 -> new address) to display test results correctly.

❗ The script is a simple replacement, so it does not work twice. Use it once and delete it.

Other useful things to know:

  1. Modify deps/requirements*.in to generate lock files in deps/lock when modified. (Actions)
  2. Create a new repo for document hosting on GitLab (e.g. ml-project-docs)
    • Refer to GitLab Pages settings documentation.
    • The GitLab address and token required for document deployment and gitlab pages are set in the Github project settings as Environment secrets / variables.
    • For open source projects, remove gitlab related parameters from .github/workflows/deploy.yml and .github/workflows/deploy-mkdocs-on-latest.yml files.
  3. Leave setup.py as it is.
  4. If you haven't written tests, delete all files in the tests/ folder to pass the tests in GitHub Actions.
  5. ⭐ Apply all contents of Python Tools in VSCode/NeoVim (formatter, linter, LSP, etc.).
  6. Remove all contents of docs/CHANGELOG.md except the top paragraph.
  7. Pre-release the first commit as v0.0.0 (possible in github release)
    • When deploying a new version with CI, there is an action to compare with the previous version, but it fails if there is no first version.
    • Please release the version when it is somewhat stable. The first stable version is v0.1.0, and the previous versions such as v0.0.1, v0.0.2 are used to test whether the release works well.

ν•œκ΅­μ–΄ README

ruff, uv, basedpyright, pytest, doctest, MkDocs, versioneer, GitHub Actions, conventional commit, changelog, typer CLI, rich logging λ“± 쒋은 것 λ‹€ μ“°λŠ” 파이썬 ν”„λ‘œμ νŠΈ ν…œν”Œλ¦Ώμž…λ‹ˆλ‹€.

λ³Έ ν…œν”Œλ¦ΏμœΌλ‘œ λ§Œλ“  νŒ¨ν‚€μ§€λŠ” λ‹€μŒκ³Ό 같이 μ‰½κ²Œ μ„€μΉ˜ν•  수 μžˆλ„λ‘ κ³ μ•ˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

# κ°œλ°œμžλŠ”
pip install -e .

# API, CLI λ‘˜ λ‹€ μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄
pip install .
pip install 'git+https://github.com/deargen/python-project-template-2024'

# CLI만 μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄
pipx install .
pipx install 'git+https://github.com/deargen/python-project-template-2024'
uv tool install .
uv tool install 'git+https://github.com/deargen/python-project-template-2024'

μ£Όμš” κΈ°λŠ₯

  1. Github Actions둜 ruff formatter, ruff linter, pytest (unit test) 톡과 μ—¬λΆ€ 확인
    • Actionsμ—μ„œ ν¬λ§€νŒ… 적용 κ°€λŠ₯
  2. GitHub Actions둜 uv pip-compile 적용 μ—¬λΆ€ 확인 (deps/requirements.in의 dynamic version을 deps/lock μ•ˆμ— lock file둜 λ³€ν™˜)
    • Actionsμ—μ„œ lock file compile κ°€λŠ₯
  3. MkDocs μ΄μš©ν•΄ μžλ™ document 생성
    • Public repoλŠ” GitHub Pages 이용
    • Private repoλŠ” GitLab Pages 이용
  4. ν”„λ‘œμ νŠΈ versioningν•˜κ³  changelog μžλ™ 생성.
  5. 편의 κΈ°λŠ₯ CLI둜 제곡
    • ml-project health둜 ν™˜κ²½ μ„€μ • 확인
    • ml-project --version으둜 ν˜„μž¬ 버전 확인

πŸƒ 돌렀 보기

  1. (Optional) pip3 install --user uv ν•΄μ„œ pip λŒ€μ‹  uv pip μ‚¬μš©ν•˜λ©΄ 더 빠름.
  2. uv pip install -r deps/lock/x86_64-manylinux_2_28/requirements.txt, uv pip install -e . 으둜 dependencies 및 ml-project νŒ¨ν‚€μ§€ μ„€μΉ˜
  3. ml-project config μ‹€ν–‰ν•΄μ„œ .env 파일 생성. (ν•„μš”ν•œ 경우 μˆ˜μ •)
  4. ml-project health μ‹€ν–‰ν•΄μ„œ ν™˜κ²½ 섀정이 잘 λ˜μ—ˆλŠ”μ§€ 확인.
  5. python tools/examples/color_logging_main.py 싀행해보기. λ‘œκΉ… λ‚΄μš©μ€ data/logs 폴더 μ•ˆμ— 기둝됨.
    • ML_PROJECT_LOG_LEVEL=WARNING python tools/examples/color_logging_main.py라고 μ‹€ν–‰ν•˜λ©΄ 좜λ ₯ λ‚΄μš©μ€‘ INFO 인것이 빠지고 좜λ ₯됨.
  6. uv pip install -r deps/lock/x86_64-manylinux_2_28/requirements_dev.txt 으둜 pytest λ“± 개발자용 νŒ¨ν‚€μ§€λ„ μ„€μΉ˜κ°€λŠ₯
  7. pytest μ»€λ§¨λ“œλ‘œ ν…ŒμŠ€νŠΈ 싀행해보기.
    • doctestλŠ” Actionsμ—μ„œ μžλ™μœΌλ‘œ 싀행됨.
  8. import ml_project; print(ml_project.__version__) 해보면 0.1.0+4.g75bbed7.dirty μ΄λŸ°μ‹μœΌλ‘œ λ‚˜μ˜΄.
    • 0.1.0 버전 이후 4개의 μ»€λ°‹μ΄λž€ 뜻. 그리고 μ»€λ°‹λ˜μ§€ μ•Šμ€ μˆ˜μ •μ‚¬ν•­μ΄ μžˆλŠ” μƒνƒœμ΄λ©΄ dirtyλ²„μ „μž„.

πŸ“° 파일 μ„€λͺ…

πŸ“‚ .vscode/
β”‚ πŸ“„ settings.json          # VSCode ruff μ €μž₯μ‹œ μžλ™ ν¬λ§€νŒ… μ„€μ • λ“±.
β”” πŸ“„ extensions.json        # VSCodeμ—μ„œ μΆ”μ²œν•  ν™•μž₯ ν”„λ‘œκ·Έλž¨ λͺ©λ‘.

πŸ“‚ .github/
β”‚ πŸ“‚ ISSUE_TEMPLATE/
β”” πŸ“‚ workflows/             # κΉƒν—™ μ•‘μ…˜ μžλ™ν™” 배포 νŒŒμ΄ν”„λΌμΈ
  β”” πŸ“„ *.yml

πŸ“‚ src/
β”” πŸ“‚ ml_project/             # `import ml_project`ν•΄μ„œ μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜λ‚˜ 클래슀 λ“± μ •μ˜ν•˜λŠ” κ³³
  β”‚ 🐍 __init__.py
  β”‚ 🐍 _version.py          # git tag둜 버전 정보λ₯Ό μ½λŠ” versioneer 파일 (μˆ˜μ •X)
  β”‚ πŸ“œ template.env         # `ml-project config` μ‹€ν–‰μ‹œ `.env`둜 λ³΅μ‚¬λ˜λŠ” ν™˜κ²½μ„€μ • 파일
  β”” 🐍 ...

πŸ“‚ tools/                   # import ν•˜μ§€ μ•Šκ³  λ°”λ‘œ μ‹€ν–‰ κ°€λŠ₯ν•œ νŒŒμΌλ“€. (예: train.py)

πŸ“‚ tests/                   # `pytest` μ‹€ν–‰μ‹œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜λ“€

πŸ“‚ scripts/                 # ν”„λ‘œμ νŠΈμ™€ 직접 κ΄€λ ¨ X, but ν”„λ‘œμ νŠΈ 관리λ₯Ό μœ„ν•΄ ν•„μš”
β”‚ # MkDocs reference νŽ˜μ΄μ§€ μƒμ„±ν• λ•Œ λͺ¨λ“ˆ μ°ΎλŠ” 슀크립트.
β”‚ # μˆ˜λ™μœΌλ‘œ μ œμ™Έν•˜κ³  싢은 λͺ¨λ“ˆ 있으면 μˆ˜μ •λ„ κ°€λŠ₯!
β”” 🐍 gen_ref_nav.py

πŸ“‚ deps/
β”‚ # 직접 μˆ˜μ • X. .github/workflows/apply-pip-compile.yml μ‹€ν–‰μ‹œ 생성됨.
β”‚ πŸ›‘οΈ .requirements.in.sha256
β”‚ πŸ›‘οΈ .requirements_dev.in.sha256
β”‚ πŸ›‘οΈ .requirements_docs.in.sha256
β”‚
β”‚ # ν”„λ‘œκ·Έλž¨ μ‚¬μš©μžμ—κ²Œ ν•„μš”ν•œ dependencies.
β”‚ πŸ–ŠοΈ requirements.in
β”‚ # ν”„λ‘œκ·Έλž¨ μ‚¬μš©μžκ°€ μ•„λ‹Œ κ°œλ°œμžμ—κ²Œ ν•„μš”ν•œ dependencies.
β”‚ πŸ–ŠοΈ requirements_dev.in
β”‚ # mkdocs둜 λ¬Έμ„œ μƒμ„±ν• λ•Œ ν•„μš”ν•œ ν”„λ‘œκ·Έλž¨λ“€
β”‚ πŸ–ŠοΈ requirements_docs.in
β”‚ # NOTE: in νŒŒμΌμ€ lock 파일이 μ•„λ‹Œ νŒ¨ν‚€μ§€ dependencyμ΄λ―€λ‘œ μ „λΆ€ dynamic version으둜 μž‘μ„±ν•΄μ•Όν•¨.
β”‚
β”‚ # 직접 μˆ˜μ • X. in νŒŒμΌμ—μ„œ 생성됨
β”” πŸ“‚ lock/
  β”‚ πŸ”’ requirements.txt
  β”‚ πŸ”’ requirements_dev.txt
  β”” πŸ”’ requirements_docs.txt

βš™οΈ pyproject.toml            # 파이썬 ν”„λ‘œμ νŠΈ 톡합 정보. ruffλ“± μ™ΈλΆ€ 툴의 섀정도 포함.

πŸ›• ν…œν”Œλ¦Ώ μ‚¬μš©ν•˜κΈ°

ν”„λ‘œμ νŠΈ 이름 λ°”κΎΈκΈ° μœ„ν•΄ ./replace_project_name.sh λ‚΄μš©μ„ μˆ˜μ • ν›„ μ‹€ν–‰ν•©λ‹ˆλ‹€. μŠ€ν¬λ¦½νŠΈμ—μ„œ μ•„λž˜ λ‚΄μš©μ„ ν•΄κ²°ν•΄μ€λ‹ˆλ‹€.

  1. src/ml_project 폴더 이름 μ›ν•˜λŠ” 걸둜 λ°”κΎΈκΈ° (import ml_project ν•  λ•Œ 이름)
  2. pyproject.toml에 λ°”κΏ”μ•Όν•˜λŠ” λΆ€λΆ„ 주석 λ˜μ–΄μžˆμŒ. λ°”κΏ” μ“°κΈ°
  3. README.md에 μžˆλŠ” badgeλ“€ URL (python-project-template-2024 -> μƒˆ μ£Όμ†Œ) λ°”κΎΈμ–΄ μ£Όμ–΄μ•Ό μ œλŒ€λ‘œ ν…ŒμŠ€νŠΈ κ²°κ³Όκ°€ 뜸.

❗ ν•΄λ‹Ή μŠ€ν¬λ¦½νŠΈλŠ” λ‹¨μˆœ μΉ˜ν™˜μ΄κΈ°μ— λ‘λ²ˆ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•œ 번 μ“°κ³  μ§€μš°μ„Έμš”.

κ·Έ μ™Έ μ•Œμ•„λ‘λ©΄ 쒋은 것듀:

  1. deps/requirements*.in을 μˆ˜μ •ν•˜λ©΄ deps/lock 폴더에 lock 파일이 생성됨. (Actions)
  2. GitLab에 document ν˜ΈμŠ€νŒ…μš© μƒˆ repo λ§Œλ“€κΈ° (예: ml-project-docs)
    • GitLab Pages μ„€μ • λ¬Έμ„œ μ°Έκ³ .
    • docs, deploy ν•  λ•Œ ν•„μš”ν•œ gitlab μ£Όμ†Œμ™€ 토큰은 Github ν”„λ‘œμ νŠΈ μ„€μ •μ—μ„œ Environment secrets / variable을 λ°”κΎΈμ–΄μ•Ό 함.
    • μ˜€ν”ˆμ†ŒμŠ€μ˜ 경우 github pageλ₯Ό μ΄μš©ν•˜λ„λ‘ .github/workflows/deploy.yml 및 .github/workflows/deploy-mkdocs-on-latest.yml νŒŒμΌμ—μ„œ gitlab κ΄€λ ¨ parameter 제거.
  3. setup.pyλŠ” κ·ΈλŒ€λ‘œ 두면 됨
  4. ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•˜μ§€ μ•Šμ€ 경우, tests/ 폴더 μ•ˆμ˜ 파일 μ „λΆ€ μ‚­μ œν•˜λ©΄ GitHub Actionsμ—μ„œ ν…ŒμŠ€νŠΈ 톡과됨.
  5. ⭐ VSCode에 Python Tools λ‚΄μš© μ „λΆ€ (formatter, linter, LSP λ“±) μ μš©ν•˜κΈ°.
  6. docs/CHANGELOG.md λŠ” 맨 μœ„ 문단 λΉΌκ³  μˆ˜μ •λ‚΄μš© λ‹€ μ‚­μ œ.
  7. 첫 commit을 v0.0.0 으둜 pre-release ν•˜κΈ° (github releaseμ—μ„œ κ°€λŠ₯)
    • μƒˆλ‘œμš΄ 버전을 CI둜 배포할 λ•Œ, 이전 버전과 λΉ„κ΅ν•˜λŠ” action이 μžˆλŠ”λ° 첫 버전이 μ—†μœΌλ©΄ 그게 μ‹€νŒ¨ν•¨.
    • μ–΄λŠ 정도 stableν•œ μƒνƒœμ—μ„œ 버전을 배포해 μ£Όμ„Έμš”. 첫 stable 버전은 v0.1.0이며 κ·Έ 이전 v0.0.1, v0.0.2 등은 릴리즈 λ™μž‘ μž˜ν•˜λŠ”μ§€ ν…ŒμŠ€νŠΈ ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.