This is a b template for Python projects develop to facilitate the creation of new projects following the best practices in the Python community.
This template can be used as a starting point for new projects.
This template uses FAST API to create the API. It is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
This template uses Docker to containerize the application. Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
This template uses Poetry to manage the dependencies. Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
This template uses MongoDB as database. MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.
This template uses Amazon S3 to store the files. Amazon S3 or Amazon Simple Storage Service is a service offered by Amazon Web Services that provides object storage through a web service interface.
This template uses Sphinx to generate the documentation. Sphinx is a tool that makes it easy to create intelligent and beautiful documentation, written by Georg Brandl and licensed under the BSD license.
This template uses Pytest to test the code. The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
We use also pytest-cov to generate the coverage report.
This template uses Docker in Docker to run the integration tests. Docker can now run within Docker. This is a feature that allows you to run Docker containers as part of your Docker build CI/CD pipeline. See section Github Actions for more details.
Indeed, if you run the tests/integration, we need to have the FastAPI server running. We can use Docker to run the FastAPI server in a container and then run the tests in another container, the github action runner.
You can see the configuration in the file .github/workflows/git_ci.yml
.
This template uses Pre-commit to run some checks before commiting the code. pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. The pre-commit hooks are run in the order given, and if any of them fail, the commit is aborted.
This template uses Github Actions to automate the workflow. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.
This template uses Loguru to log the messages. Loguru is a library which aims to bring enjoyable logging in Python. By adding a simple and powerful configuration, it allows to have a more human-friendly logging.
This template uses Flake8 to lint the code. Flake8 is a wrapper around these tools:
This template uses Black to format the code. Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.
This template uses Isort to sort the imports. isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.
This template uses Pylint to lint the code. Pylint is a tool that checks for errors in Python code, tries to enforce a coding standard and looks for code smells.
- Python 3.9+
- Poetry 1.6.1+
- Pyenv 2.3.0+
- Clone this repository
git clone https://github.com/ldom1/python-template.git
-
Create a new repository in github
-
Change the remote url to the new repository
git remote set-url origin
- Change the name of the project in the following files:
- README.md
- pyproject.toml
- Makefile
- python_template/config.py
- Install the dependencies and create the virtual environment
NB: I prefer to create my virtual environment in the project folder. If you prefer to create it in another folder, you can ignore this step and let poetry create the virtual environment in the default folder.
poetry config virtualenvs.in-project true
Then run the following commands:
pyenv install 3.10.11
pyenv shell 3.10.11
poetry env use python
source .venv/bin/activate
poetry install
pre-commit install
- Start coding!
This template is partely based on the following articles:
- Creating A Modern Python Development Environment
- Automated Project Documentation — Python
- Pytest In Practice: Fundamentals of Python Testing Techniques
- 🐍 Python Backend Project Advanced Setup (FastAPI Example)
- 4 Tools to Add to Your Python Project Before Shipping to Production
- Blazing fast Python Docker builds with Poetry
NB: Congratulations to the authors of the articles above for the quality of their work.