Environment Handlers¶
The environment handlers module (cli_env.py) contains handlers for environment management commands.
Overview¶
This module provides handlers for:
- Basic Environment Management: Create, remove, list, use, current, show
- Python Environment Management: Initialize, info, remove, shell, add-hatch-mcp
- Environment Listings: List hosts, list servers (deployment views)
Handler Functions¶
Basic Environment Management¶
handle_env_create(): Create new environmentshandle_env_remove(): Remove environments with confirmationhandle_env_list(): List environments with table outputhandle_env_use(): Set current environmenthandle_env_current(): Show current environmenthandle_env_show(): Detailed hierarchical environment view
Python Environment Management¶
handle_env_python_init(): Initialize Python virtual environmenthandle_env_python_info(): Show Python environment informationhandle_env_python_remove(): Remove Python virtual environmenthandle_env_python_shell(): Launch interactive Python shellhandle_env_python_add_hatch_mcp(): Add hatch_mcp_server wrapper
Environment Listings¶
handle_env_list_hosts(): Environment/host/server deploymentshandle_env_list_servers(): Environment/server/host deployments
Handler Signature¶
All handlers follow the standard signature:
def handle_env_command(args: Namespace) -> int:
"""Handle 'hatch env command' command.
Args:
args: Namespace with:
- env_manager: HatchEnvironmentManager instance
- <command-specific arguments>
Returns:
Exit code (0 for success, 1 for error)
"""
Module Reference¶
hatch.cli.cli_env
¶
Environment CLI handlers for Hatch.
This module contains handlers for environment management commands. Environments provide isolated contexts for managing packages and their MCP server configurations.
Commands
Basic Environment Management:
- hatch env create
Python Environment Management: - hatch env python init: Initialize Python virtual environment - hatch env python info: Show Python environment info - hatch env python remove: Remove Python virtual environment - hatch env python shell: Launch interactive Python shell - hatch env python add-hatch-mcp: Add hatch_mcp_server wrapper script
Handler Signature
All handlers follow: (args: Namespace) -> int - args.env_manager: HatchEnvironmentManager instance - Returns: EXIT_SUCCESS (0) on success, EXIT_ERROR (1) on failure
Example
$ hatch env create my-project $ hatch env use my-project $ hatch env python init $ hatch env python shell
Classes¶
Functions¶
handle_env_create(args)
¶
Handle 'hatch env create' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - name: Environment name - description: Environment description - python_version: Optional Python version - no_python: Skip Python environment creation - no_hatch_mcp_server: Skip hatch_mcp_server installation - hatch_mcp_server_tag: Git tag for hatch_mcp_server |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_current(args)
¶
Handle 'hatch env current' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success) |
Source code in hatch/cli/cli_env.py
handle_env_list(args)
¶
Handle 'hatch env list' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - pattern: Optional regex pattern to filter environments - json: Optional flag for JSON output |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success) |
Reference: R02 §2.1 (02-list_output_format_specification_v2.md)
Source code in hatch/cli/cli_env.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
handle_env_list_hosts(args)
¶
Handle 'hatch env list hosts' command.
Lists environment/host/server deployments from environment data. Shows only Hatch-managed packages and their host deployments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - env: Optional regex pattern to filter by environment name - server: Optional regex pattern to filter by server name - json: Optional flag for JSON output |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Reference: R10 §3.3 (10-namespace_consistency_specification_v2.md)
Source code in hatch/cli/cli_env.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
handle_env_list_servers(args)
¶
Handle 'hatch env list servers' command.
Lists environment/server/host deployments from environment data. Shows only Hatch-managed packages. Undeployed packages show '-' in Host column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - env: Optional regex pattern to filter by environment name - host: Optional regex pattern to filter by host name (use '-' for undeployed) - json: Optional flag for JSON output |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Reference: R10 §3.4 (10-namespace_consistency_specification_v2.md)
Source code in hatch/cli/cli_env.py
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
handle_env_python_add_hatch_mcp(args)
¶
Handle 'hatch env python add-hatch-mcp' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - hatch_env: Optional environment name (default: current) - tag: Git tag/branch for wrapper installation |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_python_info(args)
¶
Handle 'hatch env python info' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - hatch_env: Optional environment name (default: current) - detailed: Show detailed diagnostics |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_python_init(args)
¶
Handle 'hatch env python init' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - hatch_env: Optional environment name (default: current) - python_version: Optional Python version - force: Force recreation if exists - no_hatch_mcp_server: Skip hatch_mcp_server installation - hatch_mcp_server_tag: Git tag for hatch_mcp_server |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_python_remove(args)
¶
Handle 'hatch env python remove' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - hatch_env: Optional environment name (default: current) - force: Skip confirmation prompt |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_python_shell(args)
¶
Handle 'hatch env python shell' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - hatch_env: Optional environment name (default: current) - cmd: Optional command to run in shell |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Source code in hatch/cli/cli_env.py
handle_env_remove(args)
¶
Handle 'hatch env remove' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - name: Environment name to remove - dry_run: Preview changes without execution - auto_approve: Skip confirmation prompt |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Reference: R03 §3.1 (03-mutation_output_specification_v0.md)
Source code in hatch/cli/cli_env.py
handle_env_show(args)
¶
Handle 'hatch env show' command.
Displays detailed hierarchical view of a specific environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - name: Environment name to show |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |
Reference: R02 §2.2 (02-list_output_format_specification_v2.md)
Source code in hatch/cli/cli_env.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
handle_env_use(args)
¶
Handle 'hatch env use' command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Namespace with: - env_manager: HatchEnvironmentManager instance - name: Environment name to set as current |
required |
Returns:
| Type | Description |
|---|---|
int
|
Exit code (0 for success, 1 for error) |