Skip to main content

A comprehensive tool that streamlines the creation and management of Python virtual environments.

Project description

🐍 Python Environment Manager

Python 3.7+ License: MIT Build Status Code Style

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 environment
  • deactivate(): Deactivate the environment
  • get_runner(runner_type, **kwargs): Get a runner of specified type
  • remove(): Remove the environment
  • is_active(): Check if environment is active

🏃 Runners

  • RunnerFactory: Creates runner instances

    • create(name, **kwargs): Create a runner
    • register(name, runner_class): Register a custom runner
  • IRunner: Base interface implemented by all runners

    • with_env(env_manager): Configure with an environment manager
    • run(*cmd_args, **kwargs): Execute a command

📦 PackageManager

Manages packages in virtual environments.

  • install(package, **options): Install a package
  • uninstall(package, **options): Uninstall a package
  • is_installed(package): Check if a package is installed
  • list_packages(): List all installed packages
  • install_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.


Made with ❤️ by the Python Environment Manager team

GitHubPyPI

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

python_env_manager-1.0.3.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_env_manager-1.0.3-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

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

Hashes for python_env_manager-1.0.3.tar.gz
Algorithm Hash digest
SHA256 6c43591a4fc63887ce66a972103f715fd8d092d78d7f6f90e33501543e523224
MD5 987e191bbfd7066be6244c0058305170
BLAKE2b-256 91540202fee20839b883c8522565ca839a137283003abdc8fff406e248fe1622

See more details on using hashes here.

File details

Details for the file python_env_manager-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_env_manager-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3e88c3f4ef11ab3329fc7779ef63d8057927a3528edd4793c2d7057b57a50039
MD5 3f1e2e2ab9f4adca4eaf77711eb73179
BLAKE2b-256 97517ea97c2c51c0300fb575e497f7a72364080aa8ec9a0cf0375b1c93a487e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page