A flexible Python dependency manager that handles runtime dependencies and script imports.
from depkit import DependencyManager
# Simple usage
manager = DependencyManager(requirements=["requests>=2.31.0"])
manager.install()
import requests
# ... do your work ...
manager.uninstall() # optional cleanup
# Synchronous
with DependencyManager(requirements=["requests"]) as manager:
import requests
# ... do your work ...
# cleanup happens automatically
# Asynchronous
async with DependencyManager(requirements=["requests"]) as manager:
import requests
# ... do your work ...
# cleanup happens automatically
- Simple install/uninstall methods for quick usage
- Context managers for proper resource management
- PEP 723 dependency declaration support
- Support for both pip and uv package managers
- Custom pip index URL support
- Temporary script importing
- Path management for imports
pip install depkit
The DependencyManager supports both synchronous and asynchronous context managers:
from depkit import DependencyManager
async with DependencyManager(
requirements=["requests>=2.31.0", "pandas"],
prefer_uv=True
) as manager:
import requests
import pandas
from depkit import DependencyManager
with DependencyManager(
requirements=["requests>=2.31.0", "pandas"],
prefer_uv=True
) as manager:
import requests
import pandas
The DependencyManager can handle scripts with PEP 723 dependency declarations:
# example_script.py
# /// script
# dependencies = [
# "requests>=2.31.0",
# "pandas>=2.0.0"
# ]
# requires-python = ">=3.12"
# ///
import requests
import pandas as pd
Load and use the script:
async with DependencyManager(
scripts=["path/to/example_script.py"],
extra_paths=["."] # Add paths to Python's import path
) as manager:
# Script's dependencies are installed automatically
from example_script import some_function
DependencyManager(
prefer_uv: bool = False, # Prefer uv over pip if available
requirements: list[str] = None, # List of PEP 508 requirement specifiers
extra_paths: list[str] = None, # Additional Python import paths
scripts: list[str] = None, # Scripts to load and process
pip_index_url: str = None, # Custom PyPI index URL
)
The manager automatically detects and can use uv for faster package installation:
manager = DependencyManager(prefer_uv=True)
Specify a custom PyPI index:
manager = DependencyManager(
requirements=["private-package>=1.0.0"],
pip_index_url="https://private.pypi.org/simple"
)
Add custom import paths:
manager = DependencyManager(
extra_paths=[
"./src",
"./lib",
]
)
from depkit import DependencyError
try:
async with DependencyManager(requirements=["nonexistent-package"]):
pass
except DependencyError as e:
print(f"Dependency management failed: {e}")
- Use as a context manager to ensure proper cleanup
- Specify exact version requirements when possible
- Use PEP 723 for script dependencies
- Handle DependencyError exceptions appropriately
- Consider using uv in production for better performance
- Requires Python 3.12 or higher
- Some features may not work on all platforms
- UV support requires uv to be installed separately