This is a project scaffold to help you quickly get up and running with a Python package using modern, industry standard tools and practices.
The template comes with an opinionated VS Code settings configuration file to help ensure the best integration with the tools here, e.g.
- autocompletion
- syntax highlighting
- auto-formatting on save
- red/yellow squiggly lines under linting errors
- automatic discovery of tests so they are executable in the VS Code test explorer
# install cookiecutter into an isolated virtual environment
python -m venv ./venv/
source ./venv/bin/activate
pip install --upgrade pip
pip install cookiecutter
# cookie cut the template, answering the prompts, e.g.
# - repo_name: my-awesome-package
# - package_import_name: my_awesome_package
cookiecutter https://github.com/mlops-club/cloud-course-python-package-cookiecutter.gitsrc/<package-name>/- contains the package code. Using ansrc/folder is a best practice.tests/has minimal boilerplate that- Makes fixtures work using
conftest.py - Makes imports such as
from tests.my_file import thing(via some light PYTHONPATH hacking, addingtests/..to the PYTHONPATH) - Contains a
unit_test/andfunctional_test/directory whose structure is meant to mirror that ofsrc/<package>/
- Makes fixtures work using
.vscode/- contains
extensions.jsonwhich can be used to install several recommended extensions to get a great development experience - contains
settings.jsonwhich configures the various VS Code extensions, e.g.blackto read their settings frompyproject.toml
- contains
This project template makes use of
-
setuptoolsfor packaging; apip+venv+setuptoolsworkflow is encouraged as it is considered the most officially supported, "vanilla" way to manage Python packages. -
pylintandflake8for linting.ruffis up and coming, but does not yet support all the plugins and rulesets that these two do. However, we loveruffand are excitedly watching its progress.flake8supports plugins, and we are using the following-
flake8-docstrings- ensure that functions have docstrings -
flake8-pyproject- necessary forflake8to be configured using apyproject.tomlfile -
radon- a standalone tool for measuring cyclomatic complexity.Radon out-of-the-box is a
flake8plugin which enables VS Code and PyCharm to display red squiggly lines on functions whose complexity score is too high.
-
-
blackfor formatting. Note that the line length is set to119forblackand other tools that consider line length. -
pytestfor unit testing since it is the industry standard testing frameworkpytest-covfor calculating coverage and displaying an HTML coverage report in the browser
-
pre-commitframework - a convenient, industry-standard way to install and runflake8,pylint, and the other tools all at once--and in isolated virtual environments. See.pre-commit-config.yamlfor the configuration. -
Makefile- for running tasks, e.g.make lint,make test,make install.Make is unfortuanately a tool that comes pre-installed on most OSes, but it comes with its own scripting language which is limited and difficult to use compared to
bash. It supports tab-based autocompletion when running commands and can be used to chain commands together, e.g.make lint test build.This template uses
Makefileas a paper-thin wrapper overrun.shwhich allows you to author your tasks inbash, while still providing the convenience of tab-based autocompletion and chaining commands together.We are not in love with
Makefile+run.sh, but it has advantages over alternative workflows such asbashscripts are flexible. Most developers can quickly author simple bash scripts, especially with ChatGPT.Justfileis awesome, but can be difficult to install in CI and most devs do not already have it installed.PyInvokeis a thing, but requires an initial installation ofinvokewith Python, resulting in extra steps, e.g. installing in the system Python, or withpipx. It also has a slight learning curve over the first adding that much more friction to new developers joining projects.
In summary,
Makefile+run.shis a decent solution for minimized learning curve (uses ubiquitous tools), easy setup, and performance (no slight hang when executing Python-based tasks).
Notes
mypycould be added but is not present in this template
For an in-depth, step-by-step guide on the history of Python packaging and explanations of all the tools used in this scaffold, you can take the Udemy course Taking Python to Production: A Professional Onboarding Guide where we develop this template from scratch as well as many other foundational SWE skills, e.g. git, GitHub, CI/CD GitHub Actions, zsh, and testing.
- Add screenshots and GIFs to the README of the cookie cut project that demonstrate the VS Code integrations, e.g. the Test Explorer, squiggly lines, autoformatting, etc.
