Course contentsShow
← Python
Lesson 1035 of 1,06016. Capstone and AutomationFree lesson

Structuring Projects for Distribution

Learn to organize your project files and metadata using pyproject.toml to ensure compatibility with modern Python packaging standards.

In this lesson, you will learn how to standardize your project metadata using pyproject.toml to ensure your automation tools are packaged correctly for modern Python distribution.

As you build sophisticated automation tools—like the system monitors or file watchers we have explored—you eventually need to share them. In the past, Python packaging was fragmented across setup.py, setup.cfg, and requirements.txt. Today, the industry standard is to centralize everything in a single pyproject.toml file. This file tells build tools exactly how to handle your code and what dependencies are required for it to run successfully.

Organizing the Structure

To maintain a clean distribution, adopt a "src-layout" pattern. Place your source code inside a src/ directory, keeping your build configuration and tests in the root. Your project directory should look like this:

my_automation_tool/
├── pyproject.toml
├── src/
│   └── my_tool/
│       ├── __init__.py
│       └── core.py
└── tests/

The pyproject.toml File

The pyproject.toml file acts as the "source of truth." It defines the build system and project metadata. Here is a minimal example of how to declare your project's identity and dependencies:

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "my_automation_tool"
version = "0.1.0"
dependencies = [
    "rich",  # For terminal styling
    "psutil", # For resource monitoring
]

[project.scripts]
my-tool = "my_tool.core:main"

The [project.scripts] section is particularly important for your CLI tools; it maps a command name to a specific function in your code. When a user installs your package, the installer automatically generates an executable, allowing them to run your tool from anywhere in their terminal without manually calling the Python interpreter.

By using pyproject.toml, you decouple your project from specific build tools, ensuring that anyone downloading your tool—whether they use pip, poetry, or other standard installers—can reliably install and run your automation software across different environments.

Key Takeaway: Centralize your project metadata and entry points in a pyproject.toml file to ensure your automation tools are portable, installable, and compliant with modern Python packaging standards.