Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 2.86 KB

README.md

File metadata and controls

82 lines (62 loc) · 2.86 KB

py-poetry-pre-commit-template

Template for Python code using poetry and pre-commit.

python Code style: black Imports: isort

Project structure

.
├── folder                  # sample folder
│   └── file                # sample file
├── .flake8                 # `flake8` Python style enforcement config file
├── .gitignore              # ignore files that cannot be committed to Git
├── .pre-commit-config.yaml # `pre-commit` hook config file
├── poetry.lock             # lock versions of dependencies managed by poetry
├── pyproject.toml          # build system dependencies
└── README.md               # this file

Environment Setup

Installing dependencies via Poetry

poetry is a Python packaging and dependency management tool. If you don't already have poetry:

pip install poetry

Then install dependencies via poetry:

poetry install

(Note: The remaining commands assume you are in a poetry shell. If you aren't, follow the instructions in the section Activating the virtual environment.)

Activating the virtual environment

Follow the steps below to set the interpreter for the project in VSCode. This enables the IDE to read Python scripts and Jupyter notebooks.

(Note: This is a one-time process and need not be repeated afterwards.)

Create a poetry shell in the project directory:

poetry shell

It should print the path of the poetry shell in the format below (I use Python 3.8.10 on Windows 11):

Spawning shell within C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\{{path depending on project name and python version}}

Restart VSCode and open a Jupyter notebook. Click the interpreter.

Choose the path generated above, and the configuration is done.

Exit/deactivate (if needed):

exit

Pre-commit Hook

After setting up poetry, install the pre-commit hook:

pre-commit install

You can optionally run the pre-commit hook manually:

pre-commit run --all-files

Code Style

This project uses:

  • black for Python formatting
  • flake8 for Python code style
  • isort for sorting imports