Giter VIP home page Giter VIP logo

python-boilerplate's Introduction

My Minimal Python Project Boilerplate

This is a minimal Python project boilerplate I use as a starting point for new Python projects.

Prerequisites

I use pipx to install Poetry, pre-commit, and tox globally.

Installation

  1. Clone this repository.

  2. Add the following to your .bashrc or .zshrc. Note that you will need to change the value of PYTHON_BOILERPLATE to the path to the cloned repository.

    export PYTHON_BOILERPLATE="$HOME/sandbox/python-boilerplate/"
    
    # create a shell function `mkpythondir`
    mkpythondir() {( set -e
      local pyversion="${2:-$(pyenv version | cut -d' ' -f1)}"
      if [ ! -d "$(pwd)/$1" ]
      then
        rsync -r "$PYTHON_BOILERPLATE/" "$1" --exclude=".git" --exclude="README.md" # copy template files
        cd $1
        pyenv local $pyversion # set python version
        mkdir "${1//[-.]/_}" && touch "${1//[-.]/_}/__init__.py" # create package
        echo "# $1" > README.md # create README
        git init && pre-commit autoupdate && pre-commit install # git and pre-commit
        poetry init --name="$1" \
          --python="$pyversion" \
          --dev-dependency=black \
          --dev-dependency=ruff \
          --dev-dependency="docformatter[tomli]" \
          --dev-dependency=mypy \
          --no-interaction && \
          poetry env use $(pyenv version | cut -d' ' -f1) && \
          poetry install # poetry
        pyenv local --unset # unset python version
        git add . && git commit -m "Initial commit" # initial commit
      else
        echo "Directory $(pwd)/$1 already exists"
      fi
    )}

Usage

Run mkpythondir <project-name> [python-version] to create a new project. If the Python version is not specified, use the global Python version configured by pyenv or the local Python version (if configured by pyenv) in the current directory. The command does the following:

  1. Create a directory with the project name and make it a Git repository.
  2. Create an empty readme.
  3. Initialize pre-commit hooks for formatting black, docstring formatter docformatter, linter ruff, and type checker mypy.
  4. Configure tox to run formatting and linting checks.
  5. Create a Poetry virtual environment for the project with necessary development dependencies. I don't install black, docformatter, ruff, or mypy using pipx "globally" because different projects may require different versions (you and your collaborators will hate each other if using different versions of black).
  6. Make an initial commit.

IDE Configuration

Related to the above but not necessary, I also use the following configuration in my IDE:

  1. Run black and isort on save.
  2. Enable mypy and ruff linters.

This helps me to notice and fix issues before they are captured by pre-commit hooks or tox runs.

python-boilerplate's People

Contributors

tailaiw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.