Recently I was starting a Python application project from scratch and I had some issues understanding the correct project structure. Here’s what I have learnt.

Project structure

Here you can find the project structure of a python application implementing a cli tool named examplecli. You can find that app here:

├── Makefile
├── examplecli
│   ├──
│   ├── commands
│   │   ├──
│   │   ├──
│   │   ├──
│   │   └──
│   ├── common
│   │   ├──
│   │   └──
│   └──
├── requirements.txt
├── requirements_test.txt
├── scripts
│   ├──
│   └──
└── tests
    └── commands

Let’s list the most important stuff:

  • Makefile: contains a series of instructions on how to perform common tasks: clean, test, lint, coverage and building locally
  • examplecli: main module of the application. Contains all the application code organized as well in sub-modules.
  • Readme file that contains some documentation and help materials
  • requirements.txt: the pip libraries the app needs to be able to execute
  • requirements_test.txt: the pip libraries to run the app tests
  • scripts: useful tools for local development
  • we’ll discuss in a following section about this file
  • tests: folder that contains the tests for the app

Virtual environment and dependencies

In order to have a clean environment, it is very recommended to use virtual environments. This fancy feature will isolate the dependencies needed for every application.

In order to boostrap the virtual environment, you should create it (if not created) and activate it:

python -m venv .venv
source .venv/bin/activate

Once the virtual environemnt is setup, you can install the dependencies in:

pip install -r requirements.txt
pip install -r requirements_test.txt

This will store the dependencies under the .venv folder

Python modules

You should organise the python source code around the idea of modules. Modules are basically a folder with an empty file named and the source code.

it is very important to add the .py extension, otherwise it’s not recognized as a module

Different parts of the application can import the modules, e.g.:

import click

from examplecli.common.logging import debug, info
from examplecli.commands.options import verbose_option, user_option_required
def hello_source():

def hello(**opts):
    user_name = opts["user"]

def say_hello(user_name):
    debug(f"Saying hello to {user_name}")
    info(f"Hello {user_name}")

This file is importing the methods debug and info from logging file in module examplecli.common

This file is only needed if we want to package the application. Without it, we are still able to run the application by calling the entrypoint:

python3 examplecli/      

  Welcome to Example CLI!

  --help  Show this message and exit.


However, for this application, we want to package it into a wheel file and install it with pipx. In order to do that, we need the file. Let’s see what’s inside of this file:

#!/usr/bin/env python

from setuptools import setup, find_packages
from os import environ

# reads the requirements and stores them into a variable
with open('requirements.txt') as fp:
    install_requires =

# reads the test requirements and stores them into a variable
with open('requirements_test.txt') as fp:
    tests_require =

setup(name='example-cli', # name of the package
      version=environ.get('EXAMPLE_CLI_VERSION', '0.0.1'), # reads the variable from a environment variable
      description='Example CLI', # provides a description of the package
      author='Adrian Galera', # provides the author of the package
      python_requires='>=3.6.*', # details the version compatibility.
      packages=find_packages(), # the find_packages method scans the folder for modules and sub-modules
          'console_scripts': [
              'example-cli = examplecli.entrypoint:start', # required for click framework to find the starting point

In order to generate the wheel file, the user should run the following command:

EXAMPLE_CLI_VERSION=$VERSION python3 sdist bdist_wheel