
Python Project Structure
Lay out a new Python repo with src layout, cohesive modules, explicit __all__ APIs, and predictable tests before the agent sprawl gets worse.
Overview
python-project-structure is an agent skill most often used in Build (also Operate iterate when restructuring) that defines Python directory layout, module cohesion, and public APIs with __all__.
Install
npx skills add https://github.com/wshobson/agents --skill python-project-structureWhat is this skill?
- Module cohesion and single-purpose package boundaries
- Public API design using __all__ for explicit exports
- Flat-versus-nested directory guidance with src/ layout quick start
- Test placement and library packaging conventions
- Patterns for reorganizing legacy Python trees without breaking imports
Adoption & trust: 8.5k installs on skills.sh; 36.5k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Python repo is a flat pile of scripts and implicit imports, so every new feature risks breaking callers and confusing the agent about what is public.
Who is it for?
Indie developers bootstrapping a library, API service, or agent backend who want structure decided once and enforced in SKILL.md.
Skip if: Monorepos governed entirely by another language’s tooling, or one-file throwaway scripts with no planned growth.
When should I use this skill?
Setting up new Python projects, organizing modules, defining public interfaces with __all__, or planning directory and test layouts.
What do I get? / Deliverables
You get a coherent src-based layout, named subpackages, and documented public interfaces so new modules and tests land in predictable places.
- Directory tree under src/ or agreed layout
- __all__ and public module boundaries
- Documented test placement strategy
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Build is the primary shelf because the skill targets project scaffolding and module boundaries during implementation, not growth or distribution work. Backend subphase fits services, models, and package APIs rather than marketing or deploy automation.
Where it fits
Scaffold myproject/src/myproject with services, models, and api packages before writing endpoints.
Plan a folder split before adding a second agent tool without circular imports.
Refactor a scripts/ dump into cohesive modules after the MVP ships.
How it compares
Opinionated layout and API-boundary guide—not a dependency manager, formatter, or CI deploy skill.
Common Questions / FAQ
Who is python-project-structure for?
Solo builders shipping Python CLIs, APIs, or agent backends who need the agent to respect package boundaries and public exports from day one.
When should I use python-project-structure?
At the start of Build when creating pyproject and src trees; when reorganizing modules mid-project; and during Operate iterate passes when technical debt is mostly import chaos.
Is python-project-structure safe to install?
It is structural guidance without mandated shell or network tools in the SKILL.md; still review the Security Audits panel on this Prism page for the parent agents bundle.
SKILL.md
READMESKILL.md - Python Project Structure
# Python Project Structure & Module Architecture Design well-organized Python projects with clear module boundaries, explicit public interfaces, and maintainable directory structures. Good organization makes code discoverable and changes predictable. ## When to Use This Skill - Starting a new Python project from scratch - Reorganizing an existing codebase for clarity - Defining module public APIs with `__all__` - Deciding between flat and nested directory structures - Determining test file placement strategies - Creating reusable library packages ## Core Concepts ### 1. Module Cohesion Group related code that changes together. A module should have a single, clear purpose. ### 2. Explicit Interfaces Define what's public with `__all__`. Everything not listed is an internal implementation detail. ### 3. Flat Hierarchies Prefer shallow directory structures. Add depth only for genuine sub-domains. ### 4. Consistent Conventions Apply naming and organization patterns uniformly across the project. ## Quick Start ``` myproject/ ├── src/ │ └── myproject/ │ ├── __init__.py │ ├── services/ │ ├── models/ │ └── api/ ├── tests/ ├── pyproject.toml └── README.md ``` ## Fundamental Patterns ### Pattern 1: One Concept Per File Each file should focus on a single concept or closely related set of functions. Consider splitting when a file: - Handles multiple unrelated responsibilities - Grows beyond 300-500 lines (varies by complexity) - Contains classes that change for different reasons ```python # Good: Focused files # user_service.py - User business logic # user_repository.py - User data access # user_models.py - User data structures # Avoid: Kitchen sink files # user.py - Contains service, repository, models, utilities... ``` ### Pattern 2: Explicit Public APIs with `__all__` Define the public interface for every module. Unlisted members are internal implementation details. ```python # mypackage/services/__init__.py from .user_service import UserService from .order_service import OrderService from .exceptions import ServiceError, ValidationError __all__ = [ "UserService", "OrderService", "ServiceError", "ValidationError", ] # Internal helpers remain private by omission # from .internal_helpers import _validate_input # Not exported ``` ### Pattern 3: Flat Directory Structure Prefer minimal nesting. Deep hierarchies make imports verbose and navigation difficult. ``` # Preferred: Flat structure project/ ├── api/ │ ├── routes.py │ └── middleware.py ├── services/ │ ├── user_service.py │ └── order_service.py ├── models/ │ ├── user.py │ └── order.py └── utils/ └── validation.py # Avoid: Deep nesting project/core/internal/services/impl/user/ ``` Add sub-packages only when there's a genuine sub-domain requiring isolation. ### Pattern 4: Test File Organization Choose one approach and apply it consistently throughout the project. **Option A: Colocated Tests** ``` src/ ├── user_service.py ├── test_user_service.py ├── order_service.py └── test_order_service.py ``` Benefits: Tests live next to the code they verify. Easy to see coverage gaps. **Option B: Parallel Test Directory** ``` src/ ├── services/ │ ├── user_service.py │ └── order_service.py tests/ ├── services/ │ ├── test_user_service.py │ └── test_order_service.py ``` Benefits: Clean separation between production and test code. Standard for larger projects. ## Advanced Patterns ### Pattern 5: Package Initialization Use `__init__.py` to provide a clean public interface for package consumers. ```python # mypackage/__init__.py """MyPackage - A library for doing useful things.""" from .core import MainClass, HelperClass from .exceptions import Package