uv-func
Run Python functions in isolated UV virtual environments with their own dependencies – like uv run for individual functions.
Why uv-func?
Problem: Need different library versions in the same program? Want to use heavy dependencies (PyTorch, TensorFlow) in just one function? Dealing with dependency conflicts?
Solution: Decorate functions to run in isolated environments with specific dependencies, automatic caching, and real-time logging.
Quick Start
Installation
pip install uv-func
Usage
import uv_func
@uv_func.run(dependencies=["torch==2.8.0", "numpy==1.26.4"])
def torch_sum(x: list[int], y: list[int]) -> list[float]:
import torch
x_tensor = torch.tensor(x).float()
y_tensor = torch.tensor(y).float()
return (x_tensor + y_tensor).tolist() # Return serializable list
result = torch_sum([1,2,3], [4,5,6]) # [5.0, 7.0, 9.0]
Examples
Different Library Versions
@uv_func.run(dependencies=["numpy==1.21.0"])
def legacy_compute(data: list) -> float:
import numpy as np
return float(np.array(data).sum())
@uv_func.run(dependencies=["numpy==1.26.4"])
def modern_compute(data: list) -> float:
import numpy as np
return float(np.array(data, dtype=np.float32).mean())
Data Processing Pipeline
@uv_func.run(dependencies=["polars==0.20.0"])
def process_data(file_path: str) -> dict:
import polars as pl
df = pl.read_csv(file_path).filter(pl.col("value") > 100)
return {"data": df.to_dicts(), "shape": df.shape}
@uv_func.run(dependencies=["matplotlib==3.8.0", "seaborn==0.13.0"])
def visualize(data: list[float], filename: str = "plot.png") -> dict:
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(10, 6))
sns.histplot(data)
plt.savefig(filename)
plt.close()
return {"filename": filename, "points": len(data)}
How It Works
- Hash dependencies → Check cache for existing environment
- Create/reuse UV virtual environment with exact dependencies
- Serialize function + arguments with
cloudpickle - Execute in subprocess with isolated Python interpreter
- Return serialized results to parent process
⚠️ Serialization Constraints
uv-func uses cloudpickle for IPC. Keep inputs/outputs simple!
✅ Use: primitives (int, float, str, bool), collections (list, dict, tuple), simple dataclasses
❌ Avoid: complex objects, file handles, database connections, threading locks, raw library objects
# ✅ GOOD: Return dict
@uv_func.run(dependencies=["requests"])
def fetch(url: str) -> dict:
import requests
resp = requests.get(url)
return {"status": resp.status_code, "data": resp.json()}
# ❌ BAD: Return complex object
@uv_func.run(dependencies=["requests"])
def fetch_bad(url: str): # Don't do this
import requests
return requests.get(url) # Response object won't serialize
API Reference
@uv_func.run(dependencies, verbose=False)
- dependencies (
list[str]): Pip-style dependencies (e.g.,["torch==2.0.1", "numpy>=1.20"]) - verbose (
bool): Enable detailed environment setup logs - Returns: Callable executing function in isolation
Note: Arguments and returns must be cloudpickle-serializable.
Features
- 🚀 Fast: Leverages UV's speed for environment management
- 📦 Cached: Reuses environments based on dependency hash
- 🔄 Streaming: Real-time stdout/stderr from isolated functions
- 🛡️ Safe: Full exception propagation with tracebacks
- 🧵 Thread-safe: Works in multi-threaded applications
Advanced Usage
# Verbose mode for debugging
@uv_func.run(dependencies=["tensorflow==2.13.0"], verbose=True)
def train():
import tensorflow as tf
return tf.__version__
# Error handling
try:
result = some_isolated_func()
except ChildProcessError as e:
print(f"Function failed: {e}")
Requirements
- Python 3.12+
- UV package manager
Contributing
Contributions welcome! Please submit a Pull Request.
License
MIT License - see LICENSE file for details.
Related Projects
- uv - Fast Python package installer and resolver
Metadata
Release files for uv-func 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| uv_func-0.1.1.tar.gz | 8.1 kB | Without provenance | Details for uv_func-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| uv_func-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for uv_func-0.1.1-py3-none-any.whl |
Total release size: 18.1 kB
Release files / uv_func-0.1.1.tar.gz
| Download URL | uv_func-0.1.1.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d11e4277746117e7f75fa66304591b20f9fdad38f5dc1d5266e74fddd8760a97
|
|
BLAKE2b-256 checksum How to use checksums |
54af4b439f65e9d6c912e5b3904fe88f8fd578bb78189c9ee8ea7681ba1dfa26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.13
|
Release files / uv_func-0.1.1-py3-none-any.whl
| Download URL | uv_func-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca2199150c4140b39daf4a73f49e30784d662617c4c4ab18fe513261eec26c07
|
|
BLAKE2b-256 checksum How to use checksums |
b286360f28dca068f29209f55b98b17b49e7876ca9962db280546152d2076371
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.13
|
Security for uv-func 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.