Unit 10 • Lesson 3

Structuring a Python Project

Overview

Organization is key to large projects. You'll learn how to separate code into files, create modules, and use folders to keep your project clean and manageable as it grows, following professional project organization standards.

Intermediate 25–30 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Project structure: How to organize files and folders.
  • Separating concerns: Dividing code into logical modules.
  • File organization: Best practices for Python projects.
  • Module creation: Creating reusable code modules.
  • Importing modules: Using your own modules in projects.

Why Structure Matters

As projects grow, organization becomes critical. A well-structured project is:

Easier to Navigate

You can quickly find the code you need

Easier to Maintain

Changes are localized to specific files

Easier to Test

You can test individual modules separately

More Professional

Shows you understand software development

Start Simple

For small projects, a single file might be fine. But as projects grow, structure becomes essential. Start organizing early!

Basic Project Structure

Here's a simple structure for Python projects:

Example Project Structure
my_project/
├── main.py           # Entry point - runs the program
├── utils.py          # Helper functions
├── data.py           # Data handling functions
├── config.py         # Configuration settings
├── data/             # Folder for data files
│   └── tasks.csv
├── tests/            # Folder for test files
│   └── test_utils.py
└── README.md        # Project documentation

File Organization Principles

  • One main file: main.py or app.py runs your program
  • Separate by function: Group related code together
  • Use folders: Organize data, tests, and config files
  • Clear names: File names should describe their purpose

Creating Modules

Modules are Python files that contain related functions. Here's how to create and use them:

1

Create Module File

Create a .py file with related functions

2

Write Functions

Add functions that work together

3

Import in Main File

Use import to use module functions

Example: Creating and Using Modules
# utils.py - Helper functions module
def format_date(date):
    return date.strftime("%Y-%m-%d")

def validate_input(text):
    return text.strip() != ""

# main.py - Main program file
from utils import format_date, validate_input

user_input = input("Enter task: ")
if validate_input(user_input):
    print("Valid input!")
else:
    print("Input cannot be empty")

Summary

In this lesson, you learned:

  • Structure: Organize files and folders logically
  • Modules: Separate code into reusable files
  • Organization: Group related code together
  • Benefits: Easier navigation, maintenance, and testing

Remember

Good structure makes projects manageable. Even small projects benefit from organization. Start with a simple structure and expand as needed!

End-of-Lesson Exercises

Think about these questions to reinforce what you've learned:

Exercise 1: Project Structure

Why is project structure important? How would you organize a Python project?

Exercise 2: Modules

What are modules and how do you create and use them in Python?