Skip to content

MikulasZelinka/python-project-template

Repository files navigation

Python project template

A minimal, slightly opinionated template for Python projects.

Includes convenient configuration (everything is in pyproject.toml) and automatic checks (pre-commit config).

Setup

  1. (optional) If you require packages only provided by conda, first install and activate the conda environment (refer to the conda directory).

  2. Install poetry and cd to this folder.

  3. poetry install

  4. poetry run pre-commit install

You are now ready to run your project using poetry.

Usage

Running your python code

poetry run python your_script.py

Alternatively, you can activate your project env's shell to run any relevant commands in your env:

poetry shell
> pip list
> python first_script.py
> python second_script.py

Managing dependencies

Adding a dependency is as simple as running add

poetry add loguru

The dependency will then automatically be added to your pyproject.toml, although you can also work with pyproject.toml directly.

There's many more useful features (like dependency grouping), please see the poetry docs.

Git workflow

Whenever you try to git commit, the pre-commit config hooks will automatically run the tools mentioned.

Note that they only run on your currently staged files (see git status). Your unstaged files will be automatically stashed for the pre-commit run, and then unstashed back. This just means that the check is run against the file state that would actually appear in the repository if the commit succeeded.

  • If there are no changes (formatting) or problems (linting, testing) during the process, the commit will simply succeed.

  • If there are changes or linting or testing fails, the commit fails, optionally updating your files in the process (black, isort)

    • In order to then try again, first stage the changed files iff the changes look good:
      • git add -u - to add only modified but unstaged files
    • And then you can commit again, now the changes are taken into account: git commit

If for some reason you want to skip the pre-commit checks, you can do so with git commit -n (where -n is a shortcut for --no-verify). However, this is usually not advised.

In the future, we would like to have these automatic hook checks set up in our GitHub CI, so that they'd be automatically run for pull requests as well.

Approach

Guidelines

Dependencies, building, publishing

See the poetry docs.

Coding style and formatting

As dictated by black, isort and flake8.

Set and configured in pyproject.toml.

Docstrings

Use the Google docstring style (all-in-one example).

Most IDEs should support it out of the box (PyCharm, VSCode).

Logging

It is recommended to use loguru.

Additional resources:


TODO

References

About

Runs on poetry and supports both conda and pip.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published