In this lesson, you will learn how to standardize your project structure and configuration to ensure your application can be installed and executed consistently across different environments.
Establishing the Standard Layout
At this advanced stage, you’ve mastered CI/CD and containerization; now, we must ensure your source code is packaged according to the pyproject.toml standard. A robust distribution structure separates your application logic from your configuration, typically following the src/ layout. This prevents import errors and ensures that tests run against the installed package rather than local files.
Your project should look like this:
my_project/
├── pyproject.toml
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── main.py
└── tests/
Dependency Management and Entry Points
Distribution is not just about moving files; it’s about ensuring the user’s environment meets your requirements. Your pyproject.toml acts as the single source of truth for dependencies. To turn your script into an executable that a user can run directly from their terminal, you must define an entry point.
Within your pyproject.toml, you configure the [project.scripts] table. This instructs the installer (like pip) to generate a console script wrapper:
[project]
name = "my_tool"
version = "1.0.0"
dependencies = [
"requests>=2.25.1",
]
[project.scripts]
my-cli = "my_package.main:run"
In this example, when a user installs your package, the system creates a command called my-cli. This command points directly to the run() function inside your main.py module.
Why This Matters for Distribution
By using this standard configuration, you decouple your code from the environment. Whether a user installs your package via a wheel file or directly from a repository, the pip installer handles the path resolution, dependency resolution, and script creation automatically. This eliminates the "it works on my machine" problem, as the installed package behaves as a first-class citizen of the Python environment, respecting the metadata you’ve carefully defined.
Key Takeaway: Packaging your application by defining entry points and metadata in pyproject.toml transforms your source code into a portable, installable, and executable system.