$ show py2exe/py2exe
py2exe is a specialized build tool and library designed to freeze Python scripts into standalone Windows executables (.exe). It supports console applications, GUI applications, and Windows services. The architecture consists of a Python package (py2exe) that analyzes dependencies and bundles code, combined with C-based runtime stubs (MemoryModule, run.exe, etc.) that embed the Python interpreter and execute the frozen script from memory or disk. The primary entry point is the `py2exe.freeze()` API, which replaces the deprecated `distutils`/`setup.py` interface. It relies heavily on `pefile` for binary analysis and `setuptools` for packaging. The tool is actively maintained, supporting Python versions 3.10 through 3.14. It features a robust hook system for handling complex third-party packages like NumPy, SciPy, and Matplotlib. The codebase demonstrates high craftsmanship in its C integration for memory-based DLL loading and dependency scanning, making it a critical tool for distributing Python applications on Windows without requiring a Python installation.
The LLM identified 4 files worth highlighting for reuse:
The core engine of py2exe. It orchestrates the freezing process by analyzing imports via ModuleFinder, managing the creation of the library archive (zip), and handling the bundling of DLLs and resources. It implements the logic for different bundle levels (0-3) which control how extensions and the Python DLL are packaged. This module is reusable for any tool that needs to bundle Python code and dependencies into a single executable.
A collection of hooks for popular third-party packages (numpy, scipy, matplotlib, etc.). These hooks dynamically modify the module finder's behavior to include necessary data files, DLLs, or patch runtime behavior (e.g., AST rewriting for matplotlib). This pattern is highly reusable for any bundler that needs to handle complex package structures or runtime dependencies.
Implements the MemoryModule library, which allows loading DLLs and EXEs from memory without unpacking them to the file system. This is critical for creating single-file executables where dependencies are embedded in the main binary. The code handles PE header parsing, base relocations, and import table resolution in memory. It is a sophisticated implementation of a Windows-specific pattern.
Responsible for scanning binary dependencies of Python extension modules (.pyd) and DLLs. It uses `pefile` to read import tables and `BindImageEx` to resolve actual dependencies, filtering out system DLLs. This ensures all required native libraries are bundled with the executable. The logic for distinguishing between system DLLs and application-specific DLLs is a key reusable pattern for binary bundling.