DEV Community

Cover image for How I start every new Python backend API project

Posted on • Originally published at


How I start every new Python backend API project


In today's blog post I would like to show you how I approach every new Python backend API project setup.

This blog post includes:

  • project structure
  • tools
  • best practices
  • automation

Let's go! πŸš€


Poetry in my opinion is the best Python packaging and dependency management tool.

I'm using it in all of my projects since 2019.
If you have not heard about Poetry yet, I highly recommend reading about this tool.
In my opinion, it is the best option that we currently have on the Python market.

Project creation

To create a new project with poetry you need to run this command:

$ poetry new <your-project-name>
Enter fullscreen mode Exit fullscreen mode

Project structure

β”œβ”€β”€ .docker
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .pre-commit-config.yaml
β”œβ”€β”€ Makefile
β”œβ”€β”€ docs
β”‚ β”œβ”€β”€ adr
β”‚ β”‚   └──
β”‚ └── api
β”‚     └── openapi.yaml
β”œβ”€β”€ http_requests
β”‚ β”œβ”€β”€ http-client.env.json
β”‚ └── ping.http
β”œβ”€β”€ iac
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ setup.cfg
β”œβ”€β”€ src
β”‚ β”œβ”€β”€ building_blocks
β”‚ β”‚ └──
β”‚ β”œβ”€β”€ domain_module_a
β”‚ β”‚ β”œβ”€β”€ application
β”‚ β”‚ β”œβ”€β”€ domain
β”‚ β”‚ └── infrastructure
β”‚ β”œβ”€β”€ domain_module_b
β”‚ └── domain_module_c
└── tests
    β”œβ”€β”€ domain_module_a
    β”œβ”€β”€ domain_module_b
    └── domain_module_c
Enter fullscreen mode Exit fullscreen mode

I will explain the purpose of each directory, starting from the top.

πŸ“ .docker

If my project uses docker, I put all docker-related files here.
For example: init scripts for localstack.

πŸ“ docs

Inside this directory, I keep all things related to the project's documentation.

The openapi.yaml file resides inside api sub-directory.

Inside the adr sub-directory, I keep the project's ADRs.
I wrote a separate article about the ADRs, you can read it here.

πŸ“ http_requests

I use IntelliJ HTTP Client
and here is where I keep request's definitions.

If you have not heard about this tool, I recommend you check it out. Having this, you do not have to have any
additional API clients like postman or insomnia.

πŸ“ iac

If my project uses Infrastructure as code
here is where I keep terraform or other files.

πŸ“ src

Here I keep the code responsible for the application itself. This directory contains sub-directories.
I do not like splitting the project into technical layers. In opposition to that, I follow the convention
where each module is named accordingly and responsible for the business domain that it belongs to.

One of the benefits of it is that each module can have a different type of application architecture.
As you can see in the domain_module_a the separation is done by hexagonal architecture rules.
It has no impact on the other modules where such separation is not needed.

You can see there is a module called building_blocks.
Inside it, I keep all the utilities needed in the project, like a logger, serializers, and so on.
I did not make up this name, I borrowed it from this repo.

It is very important to take care of the README file because:

  • it is the first thing that will be shown after opening the project's repo
  • it allows new members of the team to start working with the project more smoothly

Here is my proposition for the README file

# <project_name>

Quick project description.

## Table of contents

* [Stack](#stack)
* [Prerequisites](#prerequisites)
* [Setup](#setup)
* [Intellij / PyCharm configuration](#intellij-/-pycharm-configuration)
* [Tests](#tests)
* [CI/CD](#ci/cd)
* [Monitoring](#monitoring)
* [ADR](#adr)
* [HTTP requests](#http-requests)

## Stack

Description of the technology stack used in the project.

## Prerequisites

Information about all needed tools you have to install before you start the development.

## Setup

Description of how to setup the project to be able to start the development.

## Architecture

Description of the project's architecture. Diagrams, maps, etc.

## Intellij / PyCharm configuration

Info on how to setup a project inside Intellij or other IDE.

## Tests

Description of how to run the tests.

## CI/CD

Description of what the CI/CD process looks like and how it works. What is the deployment strategy, etc.

## Monitoring

Information about tools used to monitor the application, how to use them, what is the purpose, how to access etc.

## ADR

Information about ADRs.

## HTTP requests

Information about IntelliJ HTTP client.

Enter fullscreen mode Exit fullscreen mode

You can find the complete README with some example descriptions for each section here:


Here is the list of tools that I always add while creating a new project:

If my project uses AWS services, I also install moto library.

I use pyproject.toml file to keep the config for each of them in one place.

This is what it looks like:

line-length = 120
target-version = ["py39"]

exclude_lines = [
    "pragma: no cover",
    "def __repr__",
    "raise AssertionError",
    "raise NotImplementedError",
    "if 0:",
    "if __name__ == .__main__.:",
    "if TYPE_CHECKING:",
fail_under = 80
show_missing = true

branch = true
omit = [

combine_as_imports = "true"
force_grid_wrap = 0
include_trailing_comma = "true"
known_first_party = "src"
line_length = 120
multi_line_output = 3

disallow_untyped_defs = true
follow_imports = "silent"
ignore_missing_imports = true
python_version = "3.9"
warn_return_any = true
warn_unused_configs = true

good-names = "id,i,j,k"

max-args = 5
max-attributes = 8
min-public-methods = 1

max-line-length = 120

[tool.pylint."MESSAGES CONTROL"]
disable = "missing-docstring, line-too-long, logging-fstring-interpolation, duplicate-code"

notes = "XXX"

ignore-comments = "yes"
ignore-docstrings = "yes"
ignore-imports = "yes"
min-similarity-lines = 6

addopts = "-v --cov=src --cov-report term-missing --no-cov-on-fail"
testpaths = ["tests"]

Enter fullscreen mode Exit fullscreen mode

The only exception is flake8 which does not support config inside pyproject.toml so we have to have an additional file
which is setup.cfg.

ignore = E501, W503, E203
max-line-length = 120
Enter fullscreen mode Exit fullscreen mode


pre-commit allows you to add git hooks that will execute before you add your commit.

This is the config file that I use in my projects:

  - repo:
    rev: v4.3.0
      - id: trailing-whitespace
      - id: check-merge-conflict
      - id: check-yaml
        args: [--unsafe]
      - id: check-json
      - id: detect-private-key
      - id: end-of-file-fixer

  - repo:
    rev: 5.10.1
      - id: isort

  - repo:
    rev: 22.8.0
      - id: black

  - repo:
    rev: 3.9.2
      - id: flake8

  - repo:
    rev: v0.971
      - id: mypy
        args: [ --warn-unused-configs, --ignore-missing-imports, --disallow-untyped-defs, --follow-imports=silent, --install-types, --non-interactive ]

Enter fullscreen mode Exit fullscreen mode


I like using Make to automate stuff in my projects.

Below you can see the Makefile that I use.


    toml-sort pyproject.toml --all --in-place

    poetry run isort .

    poetry run black .

    poetry run flake8 .

    poetry run pylint src

    docker run --rm -i hadolint/hadolint < Dockerfile

    poetry run openapi-spec-validator example-project/docs/api/openapi.yaml

    poetry run mypy --install-types --non-interactive .

    poetry export --without-hashes -f requirements.txt | poetry run safety check --full-report --stdin

    poetry run bandit -r . -x ./tests,./test

    poetry run pytest

lint: toml_sort isort black flake8 pylint mypy validate_openapi_schema

audit: audit_dependencies bandit

tests: test

all: lint audit tests

Enter fullscreen mode Exit fullscreen mode

Complete example

You can find the complete example on my GitHub πŸš€


I hope it was useful to you, and I hope that after reading this article you adopt some concepts for your project.

If you have a different opinion, please let me know. I would like to know what you think about it.

Top comments (1)

lissy93 profile image
Alicia Sykes

Nice post :)
You can also make your example repo a template (Settings --> General --> Template), which will make it easier to deploy new projects with 1-click :)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git