Kickstart Your Python Development with a Preconfigured Starter Project

Introduction

I was late to the party when it comes to Python. I started using Python in 2022 and I was surprised how easy it was to get started. I have written a few tools and one thing that I noted was that I found myself frequently starting from scratch, realizing the need for a starter project that would provide a head start. Hence, I decided to create a ‘starter project’ to accelerate the setup process for my future Python projects.

The Starter Project

The ‘starter project’ is a streamlined Python blueprint preconfigured with essential tools like:

  • unittest - Python’s built-in unit testing framework
  • behave - a behavior-driven development (BDD) framework
  • docker - a containerization tool

The starter project is available on GitHub.

The Project Structure

The project structure is as follows:

.
├── README.md
└── src # all sources in one place
    ├── container
    │   └── dockerfile # to facilitate containerized tests
    ├── features # BDD tests using behave
    │   ├── grumfundle_str.feature 
    │   └── steps
    │       └── grumfundle_str.py
    ├── requirements.txt
    ├── run_all_tests.sh # to run all tests
    ├── run_docker_all_tests.sh # to run all tests in a container
    ├── setup.py # to install the package
    ├── starter_core # the core package, ready for import
    │   ├── grumfundler.py
    │   └── __init__.py
    ├── starter.py # the main function
    └── tests # unit tests and component tests using unittest
        ├── grumfundler_component.py
        ├── grumfundler_unit.py
        └── __init__.py

Here are some key features to note:

  • there is a distinction between unit tests and component tests
  • the modules are ready for import
  • the project is ready to be installed as a package
  • all source files are neatly squared away in the src folder

Setting Up the Project

You can clone the project from GitHub:

# ssh version
git clone git@github.com:Aypahyo/python-starter.git
# https version
git clone https://github.com/Aypahyo/python-starter.git

After cloning the project, follow the instructions in the README.md file to set up the project.

Please note, there isn’t a dedicated script available to replace the default project name with your chosen name. This task would need to be performed manually.

It’s important to identify and consider all the places where the project name is used. Here is how to find all locations:

find . -name "__pycache__" -prune -o -type f -name "*starter*" -print
grep -r --exclude-dir=__pycache__ --exclude="*.log" --exclude="*.md"  "starter" .

Once you’re confident that the renaming has been carried out everywhere necessary, you’re ready to run the tests.

Running the Tests

The tests can be run individually, or all at once with the help of the provided scripts.

# run all tests individually
python3 -m unittest discover -s tests -p *_unit.py
python3 -m unittest discover -s tests -p *_component.pytests
python3 -m behave
# all: unit, component and BDD tests
src/run_all_tests.sh 
# all tests in a container and a package installation test
src/run_docker_all_tests.sh 

Conclusion

This project offers a clean module structure and gives you a leg up with ready-to-use testing setups. I hope this starter project will help you kickstart your next Python project and help you focus on the important things. If you have any questions, suggestions, or feedback, feel free to reach out to me on Twitter @Aypahyo.

Written on June 20, 2023