A comprehensive tool that streamlines the creation and management of Python virtual environments.
Project description
🐍 Python Environment Manager
A comprehensive tool that streamlines the creation and management of Python virtual environments while offering:
- Complete environment lifecycle handling from initialization through decommissioning
- Simplified dependency management via the intuitive install_pkg method
- Secure command execution within isolated environments through the user-friendly run function
- Seamless integration with modern Python development workflows
✨ Key Features
- 🔄 Virtual Environment Management - Create, activate, and remove environments
- 📦 Package Management - Manage dependencies in isolated environments
- 🛠️ Command Execution - Run commands with different runner types
- 📊 Progress Visualization - Real-time progress bars for long operations
- 🧰 Extensible Runner System - Standard, Progress, and Local runners
- 🔌 Cross-Platform - Works on Windows, macOS, and Linux
🚀 Quick Start
📋 Installation
pip install python-env-manager
Note: package name is python-env-manager while import name is env_manager.
from env_manager import EnvManager
# Create a virtual environment
env = EnvManager("my_project_env")
# Get a standard runner
runner = env.get_runner("standard")
# Install a package
runner.run("pip", "install", "requests")
# Run code with the installed package
runner.run("python", "-c", "import requests; print(requests.__version__)")
# Get a progress runner for visual feedback
progress_runner = env.get_runner("progress")
progress_runner.run("pip", "install", "tensorflow")
# Use context manager for automatic activation/deactivation
with EnvManager("another_env") as env:
runner = env.get_runner("standard")
runner.run("python", "my_script.py")
🧰 Runner Architecture
The core of the Python Environment Manager is its runner architecture, which provides different ways to execute commands.
Available Runners
- Standard Runner: Base runner for command execution
- Progress Runner: Shows real-time progress visualization
- Local Runner: Uses system Python installation
# Get different types of runners
env = EnvManager("path/to/venv")
standard_runner = env.get_runner("standard")
progress_runner = env.get_runner("progress")
local_runner = env.get_runner("local")
# Customize progress runner with inline output
custom_progress = env.get_runner("progress", inline_output=5)
📦 Package Management
Manage packages permanently or temporarily:
from env_manager import PackageManager
# Create environment and get runner
env = EnvManager("my_env")
runner = env.get_runner("standard")
# Create package manager
pkg_manager = PackageManager(runner)
# Install permanently
pkg_manager.install("requests")
# Install temporarily with context manager
with pkg_manager.install_pkg("pytest"):
runner.run("pytest", "tests/")
# Package automatically uninstalled here
🔌 Environment Activation
Three ways to work with environments:
# 1. Explicit activation/deactivation
env = EnvManager("my_env")
env.activate()
runner = env.get_runner("standard")
runner.run("python", "script.py")
env.deactivate()
# 2. Context manager (recommended)
with EnvManager("my_env") as env:
runner = env.get_runner("standard")
runner.run("python", "script.py")
# 3. Automatic activation for each command
env = EnvManager("my_env")
runner = env.get_runner("standard")
runner.run("python", "script.py") # Auto-activates for this command
🌟 Example: Data Science Workflow
from env_manager import EnvManager
# Create environment for data science
with EnvManager("data_science_env") as env:
# Get runners
std = env.get_runner("standard")
prog = env.get_runner("progress")
# Install packages with progress
prog.run("pip", "install", "numpy", "pandas", "matplotlib", "scikit-learn")
# Run analysis
std.run("python", "analyze_data.py", "--input", "data.csv", "--output", "results.csv")
📚 API Reference
🔧 EnvManager
Primary class for managing environments.
EnvManager(
path: Optional[str] = None, # Environment path (None uses system Python)
clear: bool = False, # Clear existing environment
env_builder: Optional[EnvBuilder] = None, # Custom venv builder
logger: Optional[logging.Logger] = None # Custom logger
)
Key Methods:
activate(): Activate the environmentdeactivate(): Deactivate the environmentget_runner(runner_type, **kwargs): Get a runner of specified typeremove(): Remove the environmentis_active(): Check if environment is active
🏃 Runners
-
RunnerFactory: Creates runner instancescreate(name, **kwargs): Create a runnerregister(name, runner_class): Register a custom runner
-
IRunner: Base interface implemented by all runnerswith_env(env_manager): Configure with an environment managerrun(*cmd_args, **kwargs): Execute a command
📦 PackageManager
Manages packages in virtual environments.
install(package, **options): Install a packageuninstall(package, **options): Uninstall a packageis_installed(package): Check if a package is installedlist_packages(): List all installed packagesinstall_pkg(package): Context manager for temporary installation
📊 Example 4: Using Progress Bars for Long Operations
Visualize progress for long-running operations.
from env_manager import EnvManager
# Create environment
env = EnvManager("ml_project_env")
# Install a large machine learning library with progress visualization
env.run("pip", "install", "tensorflow", progressBar=True)
# Train a model with progress visualization
env.run("python", "train_model.py", "--epochs", "100", "--batch-size", "32", progressBar=True)
# Run a data processing pipeline with progress visualization
env.run("python", "process_data.py", "--input", "large_dataset.csv", progressBar=True)
🧰 Example 5: Advanced Runner Usage
Demonstrate the flexibility of the runner architecture.
from env_manager import EnvManager, RunnerFactory, PackageManager
# Create environment manager
env_manager = EnvManager("path/to/venv")
# Get and use a standard runner
standard_runner = RunnerFactory.create("standard").with_env(env_manager)
standard_runner.run("pip", "list")
# Get and use a progress runner for long operations
progress_runner = RunnerFactory.create("progress").with_env(env_manager)
progress_runner.run("pip", "install", "large-package")
# Get and use a local runner for system Python operations
local_runner = RunnerFactory.create("local").with_env(env_manager)
local_runner.run("python", "--version")
# Use package manager for advanced package operations
pkg_manager = PackageManager(standard_runner)
pkg_manager.install("requests")
# Temporary package installation with context manager
with PackageManager(standard_runner).install_pkg("pytest"):
standard_runner.run("pytest", "--version")
📦 Requirements
- Python: 3.7+
- Dependencies: Rich (for progress bar visualization)
- Platforms: Windows, macOS, Linux
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_env_manager-1.0.3.tar.gz.
File metadata
- Download URL: python_env_manager-1.0.3.tar.gz
- Upload date:
- Size: 37.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c43591a4fc63887ce66a972103f715fd8d092d78d7f6f90e33501543e523224
|
|
| MD5 |
987e191bbfd7066be6244c0058305170
|
|
| BLAKE2b-256 |
91540202fee20839b883c8522565ca839a137283003abdc8fff406e248fe1622
|
File details
Details for the file python_env_manager-1.0.3-py3-none-any.whl.
File metadata
- Download URL: python_env_manager-1.0.3-py3-none-any.whl
- Upload date:
- Size: 26.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e88c3f4ef11ab3329fc7779ef63d8057927a3528edd4793c2d7057b57a50039
|
|
| MD5 |
3f1e2e2ab9f4adca4eaf77711eb73179
|
|
| BLAKE2b-256 |
97517ea97c2c51c0300fb575e497f7a72364080aa8ec9a0cf0375b1c93a487e1
|