Python dependency management and packaging made easy.
—
Package installation orchestration, dependency resolution, and operation management. Coordinates the installation of packages into environments with parallel processing and operation tracking.
Main installation orchestrator that coordinates dependency resolution, package downloading, building, and installation.
class Installer:
"""
Main package installation orchestrator.
Coordinates dependency resolution, package operations, and
environment management for reliable package installation.
"""
def __init__(self, io: IO, env: Env, package: ProjectPackage,
locker: Locker, pool: RepositoryPool, config: Config,
installed: InstalledRepository | None = None,
executor: Executor | None = None,
disable_cache: bool = False):
"""
Initialize installer.
Args:
io: IO interface for output
env: Target environment
package: Project package instance
locker: Lock file handler
pool: Repository pool
config: Configuration
installed: Installed repository (optional)
executor: Optional custom executor
disable_cache: Whether to disable caching
"""
@property
def executor(self):
"""Installation executor for fine-grained control."""
def set_package(self, package) -> "Installer":
"""
Set project package.
Args:
package: Project package instance
Returns:
Self for method chaining
"""
def set_locker(self, locker: Locker) -> "Installer":
"""
Set lock file handler.
Args:
locker: Locker instance
Returns:
Self for method chaining
"""
def run(self) -> int:
"""
Execute installation process.
Returns:
Exit code (0 for success)
Raises:
InstallationError: If installation fails
"""
def dry_run(self, dry_run: bool = True) -> "Installer":
"""
Set dry run mode.
Args:
dry_run: Whether to enable dry run mode
Returns:
Self for method chaining
"""
def verbose(self, verbose: bool = True) -> "Installer":
"""
Set verbose output mode.
Args:
verbose: Whether to enable verbose output
Returns:
Self for method chaining
"""
def update(self, update: bool = True) -> "Installer":
"""
Set update mode for dependencies.
Args:
update: Whether to update dependencies
Returns:
Self for method chaining
"""
def only_groups(self, groups) -> "Installer":
"""
Install only specified dependency groups.
Args:
groups: Dependency groups to install
Returns:
Self for method chaining
"""
def execute_operations(self, execute: bool = True) -> "Installer":
"""
Control whether to execute operations or just plan them.
Args:
execute: Whether to execute operations
Returns:
Self for method chaining
"""
def install_directory(self, directory: Path) -> int:
"""
Install package from directory.
Args:
directory: Path to package directory
Returns:
Exit code (0 for success)
"""from poetry.installation.installer import Installer
from poetry.factory import Factory
from poetry.utils.env import EnvManager
# Create Poetry instance and components
poetry = Factory().create_poetry()
env_manager = EnvManager(poetry.file.parent)
env = env_manager.create_venv()
# Create installer
installer = Installer(
io=None,
env=env,
pool=poetry.pool,
config=poetry.config
)
# Configure installer
installer.set_package(poetry.package)
installer.set_locker(poetry.locker)
# Run installation
exit_code = installer.run()
if exit_code == 0:
print("Installation successful")
else:
print("Installation failed")Individual installation operations representing different types of package operations.
class Install:
"""
Package installation operation.
Represents installation of a new package with
dependency handling and rollback capabilities.
"""
def __init__(self, package, reason: str = None, priority: int = 0):
"""
Initialize install operation.
Args:
package: Package to install
reason: Installation reason
priority: Operation priority
"""
@property
def package(self):
"""Package being installed."""
@property
def job_type(self) -> str:
"""Operation type identifier."""
def execute(self, env: Env) -> None:
"""
Execute installation operation.
Args:
env: Target environment
Raises:
InstallationError: If installation fails
"""
class Uninstall:
"""
Package uninstallation operation.
Represents removal of an installed package with
dependency cleanup and validation.
"""
def __init__(self, package, reason: str = None, priority: int = 0):
"""
Initialize uninstall operation.
Args:
package: Package to uninstall
reason: Uninstallation reason
priority: Operation priority
"""
def execute(self, env: Env) -> None:
"""
Execute uninstallation operation.
Args:
env: Target environment
"""
class Update:
"""
Package update operation.
Represents updating a package from one version to another
with dependency resolution and compatibility checking.
"""
def __init__(self, initial_package, target_package,
reason: str = None, priority: int = 0):
"""
Initialize update operation.
Args:
initial_package: Currently installed package
target_package: Target package version
reason: Update reason
priority: Operation priority
"""
@property
def initial_package(self):
"""Currently installed package."""
@property
def target_package(self):
"""Target package version."""
def execute(self, env: Env) -> None:
"""
Execute update operation.
Args:
env: Target environment
"""from poetry.installation.operations import Install, Update, Uninstall
# Create installation operations
install_op = Install(package, reason="new dependency")
update_op = Update(old_package, new_package, reason="version update")
uninstall_op = Uninstall(package, reason="no longer needed")
# Execute operations in environment
operations = [install_op, update_op, uninstall_op]
for operation in operations:
try:
operation.execute(env)
print(f"Executed {operation.job_type} for {operation.package.name}")
except Exception as e:
print(f"Failed {operation.job_type}: {e}")Low-level executor that handles parallel installation operations with progress tracking and error handling.
class Executor:
"""
Installation operation executor.
Handles parallel execution of installation operations
with progress tracking, error handling, and rollback.
"""
def __init__(self, env: Env, pool: RepositoryPool,
config: Config, io = None):
"""
Initialize executor.
Args:
env: Target environment
pool: Repository pool
config: Configuration
io: IO interface
"""
def execute(self, operations: list) -> None:
"""
Execute list of operations.
Args:
operations: List of installation operations
Raises:
ExecutorError: If execution fails
"""
def install(self, package) -> None:
"""
Install single package.
Args:
package: Package to install
"""
def uninstall(self, package) -> None:
"""
Uninstall single package.
Args:
package: Package to uninstall
"""
def update(self, package) -> None:
"""
Update single package.
Args:
package: Package to update
"""Build system integration for source distributions and wheel building.
class PackageBuilder:
"""
Package building utilities.
Handles building packages from source with dependency
resolution and build environment management.
"""
def __init__(self, env: Env, io = None):
"""
Initialize package builder.
Args:
env: Build environment
io: IO interface
"""
def build_wheel(self, package, dist_dir: Path) -> Path:
"""
Build wheel distribution for package.
Args:
package: Package to build
dist_dir: Distribution output directory
Returns:
Path to built wheel file
"""
def build_sdist(self, package, dist_dir: Path) -> Path:
"""
Build source distribution for package.
Args:
package: Package to build
dist_dir: Distribution output directory
Returns:
Path to built source distribution
"""
def install_from_source(self, package, env: Env) -> None:
"""
Install package from source code.
Args:
package: Package to install
env: Target environment
"""Helper functions for installation operations and package management.
def get_package_installer(package_type: str):
"""
Get installer for package type.
Args:
package_type: Type of package (wheel, sdist, etc.)
Returns:
Appropriate installer instance
"""
def validate_installation(package, env: Env) -> bool:
"""
Validate package installation in environment.
Args:
package: Package to validate
env: Environment to check
Returns:
True if installation is valid
"""
def resolve_installation_order(packages: list) -> list:
"""
Resolve installation order based on dependencies.
Args:
packages: List of packages to install
Returns:
Ordered list of packages for installation
"""from poetry.installation.installer import Installer
from poetry.config.config import Config
def configure_parallel_installation():
"""Configure installer for parallel operations."""
config = Config.create()
# Set maximum workers for parallel installation
config.merge({
"installer.max-workers": 4,
"installer.modern-installation": True
})
return config
# Usage with installer
config = configure_parallel_installation()
installer = Installer(io=None, env=env, pool=pool, config=config)from poetry.installation.installer import Installer
def install_with_groups(poetry, groups=None):
"""Install dependencies with specific groups."""
env_manager = EnvManager(poetry.file.parent)
env = env_manager.create_venv()
installer = Installer(
io=None,
env=env,
pool=poetry.pool,
config=poetry.config
)
installer.set_package(poetry.package)
installer.set_locker(poetry.locker)
# Configure for specific groups
if groups:
installer.with_groups(groups)
return installer.run()def install_development_dependencies(poetry):
"""Install project with development dependencies."""
return install_with_groups(poetry, groups=["dev", "test", "docs"])Installation system exceptions and error conditions:
class InstallationError(PoetryError):
"""Base installation error."""
class PackageNotFoundError(InstallationError):
"""Package not found during installation."""
class DependencyConflictError(InstallationError):
"""Dependency conflicts during installation."""
class BuildError(InstallationError):
"""Package building failures."""
class ExecutorError(InstallationError):
"""Installation executor errors."""
class OperationError(InstallationError):
"""Individual operation failures."""Common installation errors:
Install with Tessl CLI
npx tessl i tessl/pypi-poetry