python LogoHatch: Python Project Manager

Hatch is a modern, extensible command-line utility and build backend for Python projects, designed to streamline the development workflow from creation to distribution. It serves as a comprehensive project manager, offering features that simplify dependency management, virtual environment handling, script execution, and packaging.

Key features and concepts of Hatch include:

- PEP 517/660 Compliance: Hatch acts as a PEP 517 (for source and binary distributions) and PEP 660 (for editable installs) compliant build backend, making it a robust alternative to tools like setuptools or Poetry for defining how your project is built and packaged.
- Virtual Environments: It automatically creates and manages isolated virtual environments for your projects, ensuring that dependencies are contained and do not interfere with other projects or your system's Python installation.
- Dependency Management: You declare your project's dependencies in the `pyproject.toml` file, and Hatch handles their installation, updates, and removal within the project's virtual environment.
- Script Execution: Hatch allows you to define custom scripts within your `pyproject.toml` configuration. These scripts can then be executed easily using `hatch run <script_name>`, running them within the correct project environment.
- Build System: It facilitates the creation of distributable packages (source distributions and wheels), ready for publishing to package indexes like PyPI.
- Plugins and Extensibility: Hatch is designed with extensibility in mind, allowing for custom environments, build hooks, and more through a plugin system.
- Configuration: All project configuration, including metadata, dependencies, scripts, and build settings, is managed through the standard `pyproject.toml` file, adhering to modern Python packaging standards.

By unifying various project management tasks under a single, consistent interface, Hatch helps developers maintain reproducible environments, simplify collaboration, and accelerate the packaging and publishing process for their Python applications and libraries.

Example Code

To demonstrate Hatch's core functionalities, let's walk through creating a project, adding a dependency, defining a script, and building it.

1. Install Hatch:
   First, ensure Hatch is installed globally:
   bash
   pip install hatch
   

2. Create a new project:
   This command generates a new project structure with a default `pyproject.toml` and basic files.
   bash
   hatch new my-hatch-project
   cd my-hatch-project
   

3. Modify `pyproject.toml`:
   Open the `pyproject.toml` file in your `my-hatch-project` directory and update it to include an example dependency (`rich`) and define a custom script `hello` and `greet-user`.

   -Before (example minimal `pyproject.toml` content after `hatch new`):-
   toml
    ... existing project metadata ...
   [project]
   name = "my-hatch-project"
   version = "0.0.1"
   dependencies = []  This might be empty or contain initial deps

   [tool.hatch.envs.default]
    Default environment configuration, e.g., test dependencies
   dependencies = [
       "pytest",
       "pytest-cov",
   ]
    ... other configurations ...
   

   -After (adding `rich` to project dependencies and defining custom scripts):-
   toml
    ... existing project metadata ...
   [project]
   name = "my-hatch-project"
   version = "0.0.1"
   dependencies = [
       "rich",  Add 'rich' as a project dependency
   ]

   [tool.hatch.envs.default]
   dependencies = [
       "pytest",
       "pytest-cov",
   ]

   [tool.hatch.scripts]
    Define a script named 'hello' that uses the 'rich' library
   hello = "python -c \"from rich.console import Console; console = Console(); console.print('[bold green]Hello[/bold green], [blue]Hatch![/blue]')\"
    Define another script named 'greet-user' that prompts for input
   greet-user = "python -c \"name = input('Enter your name: '); print(f'Hello, {name}!')\"
    ... other configurations ...
   
   -Explanation:-
   - We added `"rich"` to `project.dependencies`. Hatch will ensure this is installed in the project's virtual environment when needed.
   - Under `[tool.hatch.scripts]`, we defined two custom commands: `hello` (which utilizes `rich` to print colorful text) and `greet-user` (which takes user input).

4. Run a custom script:
   Hatch automatically sets up or activates the project's virtual environment and executes the specified script.
   bash
   hatch run hello
   
   You should see: `Hello, Hatch!` (displayed in green and blue text, if your terminal supports it).

   To run the other script:
   bash
   hatch run greet-user
   
   You will be prompted to `Enter your name: ` in the console.

5. Build the project:
   This command creates distribution archives (source distribution `sdist` and Python wheels `wheel`) in the `dist/` directory, ready for packaging or publishing.
   bash
   hatch build
   

6. Clean build artifacts:
   Removes the `dist/` directory and other build-related temporary files/directories.
   bash
   hatch clean
   

This example demonstrates how Hatch facilitates dependency management, script definition and execution, and project building, all efficiently managed through the `pyproject.toml` configuration.