# pyenv/pyenv-virtualenv architecture

> Architecture diagram of the GitHub repository pyenv/pyenv-virtualenv, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pyenv/pyenv-virtualenv
- Repository: https://github.com/pyenv/pyenv-virtualenv

## Overview

##### Main Components

a) Core Components:
- pyenv (parent system)
- pyenv-virtualenv plugin
- Virtual Environment Management System
- Environment Handlers (virtualenv, venv, conda)

b) Integration Points:
- Shell Integration
- pyenv Hooks System
- Environment Activation/Deactivation System

**User Shell Environment**
- ↕
- pyenv-virtualenv
- ↕
- pyenv core
- ↕
**Virtual Environment Backends**

##### Component Details

a) Command Interface Layer:
- Bin/ directory commands (activate, deactivate, virtualenv, etc.)
- To the shell integration system

b) Core Processing Layer:
- Libexec/ directory components
- Etc/pyenv.d/ hooks system
- Demonstrate how the plugin integrates with pyenv

c) Backend Integration Layer:
- Three parallel paths for different virtual environment types:
- virtualenv
- venv (Python 3.3+)
- conda environments

##### Data Flow Indicators

- Command flow from shell to plugin
- Plugin interactions with pyenv
- Virtual environment creation/management flow
- Activation/deactivation state changes

##### Implementation Details

- Hook system implementation (etc/pyenv.d/)
- Test infrastructure (test/ directory)
- Shell integration points (sh-activate, sh-deactivate)

##### Specific Elements

**┌─ Shell Environment ─────────────────┐**
- (bash/zsh/fish integration)         │
- ┬────────────────────┘
- ↓
**┌─ Command Interface ────────────────┐**
- pyenv virtualenv                 │
- pyenv activate/deactivate        │
- pyenv virtualenvs               │
- ┬──────────────────┘
- ↓
**┌─ Core Plugin Logic ───────────────┐**
- Environment Management          │
- Hook System                     │
- Auto-activation                 │
- ┬──────────────────┘
- ↓
**┌─ Backend Handlers ────────────────┐**
- ┌─────────┐ ┌───────┐ ┌────────┐ │
- virtualenv│ │ venv  │ │ conda  │ │
- ┘ └───────┘ └────────┘ │
- ┘

##### Additional Notes

- Clear boundaries between pyenv core and plugin functionality
- Auto-activation feature as it's a key differentiator
- Configuration points for environment variables

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Shell["Shell Environment"]
        ShellInt["Shell Integration"]
        ShInit["Shell Initialization"]:::shell
        ShAct["Shell Activation"]:::shell
        ShDeact["Shell Deactivation"]:::shell
    end

    subgraph Commands["Command Interface"]
        MainCmd["Main Command Interface"]:::cmd
        EnvAct["Environment Activation"]:::cmd
        EnvDeact["Environment Deactivation"]:::cmd
        EnvList["Environment Listing"]:::cmd
        EnvDel["Environment Deletion"]:::cmd
    end

    subgraph Core["Core Processing Layer"]
        PluginLogic["Plugin Logic"]
        PathRes["Path Resolution"]:::core
        EnvPrefix["Environment Prefix Management"]:::cmd
        AutoAct["Auto-activation System"]
    end

    subgraph Hooks["Hook System"]
        HookDir["Hooks Directory"]:::hook
        RehashHook["Rehash Hooks"]:::hook
        UninstallHook["Uninstall Hooks"]:::hook
    end

    subgraph Backend["Backend Integration Layer"]
        CondaBack["Conda Backend"]:::backend
        PythonConfig["Python Config"]:::backend
        SitePackages["Site Packages"]:::backend
    end

    subgraph Testing["Test Infrastructure"]
        TestSuite["Main Test Suite"]:::test
        CondaTest["Conda Tests"]:::test
        VenvTest["Virtualenv Tests"]:::test
        PyVenvTest["PyVenv Tests"]:::test
    end

    Shell --> Commands
    Commands --> Core
    Core --> Hooks
    Core --> Backend
    Backend --> Testing

    %% Click events for component mapping
    click MainCmd "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-virtualenv"
    click EnvAct "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-activate"
    click EnvDeact "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-deactivate"
    click EnvList "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-virtualenvs"
    click EnvDel "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-virtualenv-delete"
    click ShAct "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-sh-activate"
    click ShDeact "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-sh-deactivate"
    click ShInit "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-virtualenv-init"
    click HookDir "https://github.com/pyenv/pyenv-virtualenv/tree/master/etc/pyenv.d/"
    click RehashHook "https://github.com/pyenv/pyenv-virtualenv/blob/master/etc/pyenv.d/rehash/envs.bash"
    click UninstallHook "https://github.com/pyenv/pyenv-virtualenv/blob/master/etc/pyenv.d/uninstall/envs.bash"
    click CondaBack "https://github.com/pyenv/pyenv-virtualenv/blob/master/etc/pyenv.d/which/conda.bash"
    click PythonConfig "https://github.com/pyenv/pyenv-virtualenv/blob/master/etc/pyenv.d/which/python-config.bash"
    click SitePackages "https://github.com/pyenv/pyenv-virtualenv/blob/master/etc/pyenv.d/which/system-site-packages.bash"
    click PathRes "https://github.com/pyenv/pyenv-virtualenv/tree/master/libexec/pyenv-virtualenv-realpath"
    click EnvPrefix "https://github.com/pyenv/pyenv-virtualenv/tree/master/bin/pyenv-virtualenv-prefix"
    click TestSuite "https://github.com/pyenv/pyenv-virtualenv/tree/master/test/"
    click CondaTest "https://github.com/pyenv/pyenv-virtualenv/blob/master/test/conda.bats"
    click VenvTest "https://github.com/pyenv/pyenv-virtualenv/blob/master/test/virtualenv.bats"
    click PyVenvTest "https://github.com/pyenv/pyenv-virtualenv/blob/master/test/pyvenv.bats"

    %% Styling
    classDef shell fill:#6b8e23,stroke:#333,stroke-width:2px
    classDef cmd fill:#4682b4,stroke:#333,stroke-width:2px
    classDef core fill:#32cd32,stroke:#333,stroke-width:2px
    classDef hook fill:#daa520,stroke:#333,stroke-width:2px
    classDef backend fill:#ff8c00,stroke:#333,stroke-width:2px
    classDef test fill:#808080,stroke:#333,stroke-width:2px
```
