Skip to main content

Wrapper to manage Python venvs & run scripts, installing missing dependencies

Project description

PyEnvRunner

Automatically manage Python virtual environments and dependencies.

PyEnvRunner is a command-line tool that runs Python scripts with automatic dependency detection and installation. When your script imports missing packages, PyEnvRunner automatically installs them in an isolated virtual environment. Perfect for quick prototyping, sharing scripts, and avoiding "works on my machine" issues.

PyPI version License: MIT Python 3.6+

โœจ Features

  • ๐Ÿš€ Automatic Dependency Installation - Detects missing imports at runtime and installs them automatically
  • ๐Ÿ”’ Isolated Environments - Creates virtual environments to keep your system Python clean
  • ๐Ÿ’จ Real-time Output Streaming - See script output immediately, perfect for long-running services and APIs
  • ๐Ÿ“ฆ Import Name Mapping - Handles cases where import names differ from PyPI names (e.g., import cv2 โ†’ pip install opencv-python)
  • ๐Ÿ“ Requirements Tracking - Optionally save installed packages to a requirements file with version pinning
  • ๐ŸŽฏ Zero External Dependencies - Uses only Python standard library
  • ๐Ÿ”ง Flexible CLI - Supports various workflows with intuitive command-line flags

๐ŸŽฏ Use Cases

  • Quick Prototyping - Run scripts without worrying about dependencies
  • Sharing Scripts - Share a single .py file that anyone can run
  • CI/CD Pipelines - Automatically install dependencies in clean environments
  • API Development - Real-time output streaming for server logs
  • Education - Students can run examples without manual setup
  • Testing - Quickly test scripts in isolated environments

๐Ÿ“ฆ Installation

pip install pyenvrunner

Or install from source:

git clone https://github.com/yourusername/pyenvrunner.git
cd pyenvrunner
pip install -e .

๐Ÿš€ Quick Start

Basic Usage

# Run a script - automatically installs missing packages
pyenvrunner my_script.py

# Run a script with arguments
pyenvrunner my_script.py --arg1 value1 --arg2 value2

# Save installed packages to requirements file
pyenvrunner --save-reqs my_script.py

Example Script

Create a file demo.py:

# No need to install requests first!
import requests

response = requests.get('https://api.github.com')
print(f"Status: {response.status_code}")

Run it:

$ pyenvrunner demo.py

Virtual environment is ready in './env'.
To activate it manually in your shell:
  source env/bin/activate
------------------------------

--- Running script: demo.py using env/bin/python ---
Traceback (most recent call last):
  File "demo.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

>>> Detected missing module: 'requests'. Attempting to install 'requests'...
--- pip install STDOUT for requests ---
Collecting requests
  Using cached requests-2.32.5-py3-none-any.whl (64 kB)
...
Successfully installed requests-2.32.5
>>> Installation of requests successful. Retrying script...

--- Running script: demo.py using env/bin/python ---
Status: 200

--- Script demo.py completed successfully ---

๐Ÿ“– CLI Documentation

Command Syntax

pyenvrunner [OPTIONS] <script_path> [SCRIPT_ARGS...]

Options

Script Execution

  • script_path - Path to the Python script to execute (default: main.py)

Virtual Environment Options

  • --env-name NAME - Name of the virtual environment directory (default: env)
  • --use-current-env - Use the current Python environment instead of creating a venv
  • --force-recreate-env - Delete and recreate the venv if it already exists

Package Management Options

  • --save-reqs - Save newly installed packages to requirements file
  • --reqs-file FILE - Specify custom requirements file name (default: pyenvrunner_requirements.txt)

Utility Commands

  • --list-import-mappings - Display predefined import-to-package mappings and exit
  • --clear-env - Remove all installed packages from the environment and exit
  • -h, --help - Show help message and exit

Usage Examples

# Basic execution with automatic venv creation
pyenvrunner my_script.py

# Save dependencies to requirements file
pyenvrunner --save-reqs my_script.py

# Use custom virtual environment name
pyenvrunner --env-name my_custom_env my_script.py

# Force recreate environment (fresh start)
pyenvrunner --force-recreate-env my_script.py

# Use current Python environment (no venv)
pyenvrunner --use-current-env my_script.py

# Save to custom requirements file
pyenvrunner --save-reqs --reqs-file deps.txt my_script.py

# Pass arguments to your script
pyenvrunner my_script.py --input data.csv --output results.json

# List import name mappings
pyenvrunner --list-import-mappings

# Clear all packages from environment
pyenvrunner --clear-env --env-name my_env

๐Ÿ—บ๏ธ Import Name Mapping

Some Python packages have different import names than their PyPI package names. PyEnvRunner handles these automatically:

Import Statement PyPI Package
import cv2 opencv-python
import sklearn scikit-learn
from PIL import Image Pillow
import yaml PyYAML
from dotenv import load_dotenv python-dotenv
import dateutil python-dateutil

View all mappings:

pyenvrunner --list-import-mappings

๐Ÿ”ง How It Works

  1. Create/Reuse Virtual Environment - PyEnvRunner creates a venv in the current directory (or uses an existing one)
  2. Run Your Script - Executes your script using the venv's Python interpreter
  3. Detect Missing Imports - Monitors stderr for ModuleNotFoundError messages
  4. Install Packages - Automatically runs pip install <package> for missing modules
  5. Retry Execution - Reruns the script after installing packages
  6. Real-time Output - Streams stdout/stderr in real-time using multi-threading
  7. Success - Script completes with all dependencies installed

๐Ÿ“ Project Structure

pyenvrunner/
โ”œโ”€โ”€ pyenvrunner/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli/
โ”‚   โ”‚   โ””โ”€โ”€ main.py              # CLI entry point and argument parsing
โ”‚   โ””โ”€โ”€ core/
โ”‚       โ”œโ”€โ”€ config.py            # Configuration (import mappings, defaults)
โ”‚       โ”œโ”€โ”€ exceptions.py        # Custom exception classes
โ”‚       โ”œโ”€โ”€ package_management.py # Package installation and script execution
โ”‚       โ””โ”€โ”€ venv_management.py   # Virtual environment creation
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_cases/              # 20 comprehensive test cases
โ”‚   โ”œโ”€โ”€ run_tests.py             # Test runner with reporting
โ”‚   โ””โ”€โ”€ clear.sh                 # Cleanup script
โ”œโ”€โ”€ setup.py                     # Package configuration
โ”œโ”€โ”€ .gitignore                   # Git ignore rules
โ””โ”€โ”€ README.md                    # This file

๐Ÿงช Testing

PyEnvRunner includes a comprehensive test suite with 20 test cases covering all features:

cd tests
python3 run_tests.py

Test categories:

  • Core Functionality - Basic execution, argument handling, auto-install
  • CLI Flags - All command-line options
  • Error Handling - Script errors, invalid imports, exit codes
  • Edge Cases - No output, special characters, warnings

Clean up test artifacts:

cd tests
./clear.sh

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for your changes
  5. Run the test suite (cd tests && python3 run_tests.py)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Development Setup

git clone https://github.com/yourusername/pyenvrunner.git
cd pyenvrunner
pip install -e .

๐Ÿ“‹ Requirements

  • Python 3.6 or higher
  • No external dependencies (uses only Python standard library)

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Inspired by tools like pipx, uvx, and npm's automatic package installation
  • Built with โค๏ธ for the Python community

๐Ÿ› Known Limitations

  • Import Detection - Only detects ModuleNotFoundError at runtime. Imports inside try-except blocks won't be detected.
  • Dynamic Imports - Doesn't detect imports using importlib or __import__().
  • Sequential Installation - Installs packages one at a time, not in parallel.

๐Ÿ“ž Support

๐Ÿ—บ๏ธ Roadmap

  • Add static analysis to detect imports before runtime
  • Support for conda environments
  • Parallel package installation
  • Configuration file support (.pyenvrunner.toml)
  • Integration with poetry/pipenv
  • Docker container support

Made with โค๏ธ by Aditya Thiyyagura

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

pyenvrunner-1.0.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

pyenvrunner-1.0.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file pyenvrunner-1.0.0.tar.gz.

File metadata

  • Download URL: pyenvrunner-1.0.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.10

File hashes

Hashes for pyenvrunner-1.0.0.tar.gz
Algorithm Hash digest
SHA256 620d39a29f1a8ac717abf591c978d32514ece4589093c316debe08cfef713f20
MD5 5607ecac2e8d686318d9f2a5912be237
BLAKE2b-256 2f11bb48a03b543b1c618f3e475ab6d9e8baea9dc0c5bbc266d995eecd9066f0

See more details on using hashes here.

File details

Details for the file pyenvrunner-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyenvrunner-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.10

File hashes

Hashes for pyenvrunner-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8de746c8732f6d77dce0f2467bb3415d393bfd4e0e842df7641dc8be73a18474
MD5 34fbb12c777d641c13873ed3be9a51db
BLAKE2b-256 29389889a82b84e5b6c9ff275b458a2d8e7ee2dc91055501b098395d7eac430e

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