β Beginner-friendly | π₯ Hands-on Guide | π§© File Structure Examples
When you start working on Python projectsβwhether for AWS Lambda, building an API, or creating a CLI toolβit’s important to organize your code in a clean and simple way.
A well-structured project is easy to maintain, scale, and share with others.
In this post, I will show common file and folder structures I personally use in my projects. These structures are helpful for newcomers, junior devs, or even freelancers who want to start clean from the beginning.
π 1. Python Project for AWS Lambda (Multiple Files)
When working with AWS Lambda, you often split your code into reusable files. This makes your handler clean and easier to manage.
π Folder Structure:
your-lambda-project/ βββ src/ β βββ lambda_function.py # Main Lambda handler β βββ config.py # Environment or API config β βββ logger.py # Custom logger setup β βββ utils.py # Helper functions β βββ services.py # AWS or API calls β βββ validator.py # Input validations β βββ exceptions.py # Custom error handling β βββ constants.py # Static values β βββ integration.py # API/database integration logic βββ requirements.txt # Dependencies βββ serverless.yml # Serverless Framework (optional) βββ Dockerfile # If you deploy using containers βββ README.md βββ CHANGELOG.md
π 2. Python Project for API (Flask or FastAPI)
Building RESTful APIs with Flask or FastAPI? You can group files like this for better readability and code separation.
π Folder Structure:
your-api-project/ βββ src/ β βββ main.py # Entry point to run the app β βββ routes.py # API routes/endpoints β βββ controllers.py # Logic for each route β βββ services.py # Handle DB/API calls β βββ models.py # Pydantic or DB models β βββ config.py β βββ logger.py β βββ utils.py β βββ exceptions.py β βββ constants.py βββ requirements.txt βββ .env # Environment variables βββ Dockerfile # Container support (optional) βββ README.md βββ CHANGELOG.md
π 3. Python CLI Tool Project
Python is also great for building command-line tools. You can use argparse
or click
to handle user inputs.
π Folder Structure:
your-cli-tool/ βββ src/ β βββ main.py # CLI entry script β βββ cli.py # Argument parsing or click commands β βββ commands.py # CLI command logic β βββ utils.py β βββ logger.py β βββ config.py β βββ constants.py β βββ exceptions.py βββ setup.py # For making your CLI installable βββ requirements.txt βββ Dockerfile # Optional for packaging βββ README.md βββ CHANGELOG.md
π οΈ Tips for Beginners
- β
Use
src/
folder to keep your Python code separate. - β
Keep your
requirements.txt
updated. - β
Always add
README.md
to explain how your code works. - β Group similar logic into their own files (e.g., services, utils).
- β
Add
Dockerfile
only if needed (for deployment or testing). - β
If you use Serverless Framework, include
serverless.yml
.
β¨ Final Thoughts
Whether you are a beginner, a junior dev, or a self-learner like meβstarting with the right folder structure helps you write better code, collaborate easier, and debug faster.