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.
โจ 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
.pyfile 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
- Create/Reuse Virtual Environment - PyEnvRunner creates a venv in the current directory (or uses an existing one)
- Run Your Script - Executes your script using the venv's Python interpreter
- Detect Missing Imports - Monitors stderr for
ModuleNotFoundErrormessages - Install Packages - Automatically runs
pip install <package>for missing modules - Retry Execution - Reruns the script after installing packages
- Real-time Output - Streams stdout/stderr in real-time using multi-threading
- 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:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Run the test suite (
cd tests && python3 run_tests.py) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
ModuleNotFoundErrorat runtime. Imports inside try-except blocks won't be detected. - Dynamic Imports - Doesn't detect imports using
importlibor__import__(). - Sequential Installation - Installs packages one at a time, not in parallel.
๐ Support
- Issues - GitHub Issues
- Email - thiyyaguraadityareddy@gmail.com
๐บ๏ธ 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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
620d39a29f1a8ac717abf591c978d32514ece4589093c316debe08cfef713f20
|
|
| MD5 |
5607ecac2e8d686318d9f2a5912be237
|
|
| BLAKE2b-256 |
2f11bb48a03b543b1c618f3e475ab6d9e8baea9dc0c5bbc266d995eecd9066f0
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8de746c8732f6d77dce0f2467bb3415d393bfd4e0e842df7641dc8be73a18474
|
|
| MD5 |
34fbb12c777d641c13873ed3be9a51db
|
|
| BLAKE2b-256 |
29389889a82b84e5b6c9ff275b458a2d8e7ee2dc91055501b098395d7eac430e
|