Work with Virtual Environments
Angreal provides virtual environment management using UV, a fast Python package installer and resolver.
Overview
Section titled “Overview”The virtual environment integration provides:
- Create and manage virtual environments
- Install packages and requirements
- Ensure specific Python versions
- Automatic environment activation for tasks
- Programmatic activation within Python processes
Basic Usage
Section titled “Basic Usage”from angreal.integrations.venv import VirtualEnv
# Create a new environmentvenv = VirtualEnv("myproject-env")
# Install packagesvenv.install(["django", "requests"])
# Install from requirements filevenv.install("requirements.txt")Activating Virtual Environments
Section titled “Activating Virtual Environments”Manual Activation
Section titled “Manual Activation”from angreal.integrations.venv import VirtualEnv
# Create and activate a virtual environmentvenv = VirtualEnv("data-env", now=True)venv.install(["numpy", "pandas"])
# Activate the environmentvenv.activate()
# Now you can import the installed packagesimport numpy as npimport pandas as pd
# Remember to deactivate when donevenv.deactivate()Using Context Manager
Section titled “Using Context Manager”from angreal.integrations.venv import VirtualEnv
# Automatic activation/deactivation with context managerwith VirtualEnv("analysis-env", now=True) as venv: venv.install("scikit-learn")
# Environment is activated here from sklearn import datasets iris = datasets.load_iris()
# Environment is automatically deactivated hereUsing with Tasks
Section titled “Using with Tasks”from angreal.integrations.venv import venv_requiredimport angreal
@angreal.command(name="analyze", about="Run data analysis")@venv_required(".venv", requirements=["pandas", "numpy"])def analyze_data(): """This function runs in an isolated environment with packages available.""" import pandas as pd import numpy as np # Your code here - the virtual environment is activatedPython Version Management
Section titled “Python Version Management”from angreal.integrations.venv import VirtualEnv
# Create environment with specific Python versionvenv = VirtualEnv("py312-env", python="3.12")
# List available Python versionspythons = VirtualEnv.discover_available_pythons()for version, path in pythons: print(f"{version}: {path}")Related Documentation
Section titled “Related Documentation”- Virtual Environment API Reference - Complete API documentation
- Create Tasks - How to integrate environments with tasks
Next Steps
Section titled “Next Steps”- Read the Virtual Environment API Reference for complete API documentation
- Learn about UV Integration Architecture for implementation details
- Explore Creating Tasks to integrate environments with your automation workflows