Project Organization Best Practices
Overview
Good project structure makes collaboration easier and code more maintainable. You'll learn how to organize files into logical directories, use proper naming conventions, and follow real-world Python package design standards, setting you up for professional development.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Directory structure: Learn how to organize files into logical folders.
- Naming conventions: Understand Python naming best practices.
- Package structure: Discover how to structure Python packages properly.
- Project templates: Learn standard project organization patterns.
Why This Matters
As your projects grow, organization becomes critical. A well-organized project is easier to understand, maintain, and share. Following best practices makes your code look professional and helps others (and future you) understand your project quickly. Good organization is a sign of experienced developers!
Step 1: Basic Project Structure
Here's a standard way to organize a Python project:
myproject/
├── README.md # Project description
├── requirements.txt # Dependencies
├── setup.py # Package setup (optional)
├── main.py # Entry point
├── .gitignore # Git ignore file
├── src/ # Source code
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
├── tests/ # Test files
│ ├── __init__.py
│ └── test_module1.py
└── docs/ # Documentation
└── README.md
Root Directory
The project folder contains configuration files (README.md, requirements.txt) and an entry point (main.py).
Source Code
Put your main code in a src folder or directly in the project root. Organize modules logically.
Tests
Keep test files in a separate tests folder. This keeps code and tests organized.
Key Concept: A well-organized project has a clear structure. Related files are grouped together, configuration is at the root, and documentation is accessible. This makes it easy to find what you need!
Step 2: Naming Conventions
Python has conventions for naming files, folders, and code:
# File and folder names: lowercase with underscores
# Good:
my_module.py
data_processing.py
utils/
# Bad:
MyModule.py
DataProcessing.py
Utils/
# Variable and function names: lowercase with underscores
def calculate_total():
user_name = "Alice"
# Class names: PascalCase
class UserAccount:
pass
Files and Folders
Use lowercase with underscores: my_module.py, data_utils/
Variables and Functions
Use lowercase with underscores: user_name, calculate_total()
Classes
Use PascalCase: UserAccount, DataProcessor
Constants
Use UPPERCASE with underscores: MAX_SIZE, DEFAULT_VALUE
Step 3: Organizing Code
Group related functionality together:
# Group related functions in modules
# math_utils.py - all math-related functions
def add(a, b):
return a + b
def multiply(a, b):
return a * b
# string_utils.py - all string-related functions
def capitalize(text):
return text.capitalize()
def reverse(text):
return text[::-1]
Organization Principles
- Group related code together in modules
- Keep modules focused on one purpose
- Separate concerns (data processing, UI, business logic)
- Use packages for larger projects
Step 4: Essential Files
Every project should have these files:
README.md
Describes what the project does and how to use it. Essential for sharing projects!
requirements.txt
Lists all package dependencies. Others can install them with pip install -r requirements.txt.
.gitignore
Tells Git which files to ignore (like virtual environments, cache files, etc.).
LICENSE
Specifies how others can use your code. Important for open-source projects!
End-of-Lesson Exercises
Exercise 1: Project Structure Concepts
Create comments explaining what each folder in a standard project structure contains: src/, tests/, and docs/. Then print a message explaining why good project organization matters.
Write comments explaining each folder, then print an explanation.
Exercise 2: Naming Conventions
Create a comment explaining Python naming conventions. Then create a function with a proper name (using lowercase with underscores) that calculates the square of a number. Call it with 5 and print the result.
Write a comment about naming, then create a function following conventions.