Metadata-Version: 2.1
Name: uvtrick
Version: 0.4.1
Summary: A fun party trick to run Python code from another venv into this one.
Author: Vincent D Warmerdam
License: MIT License
        
        Copyright (c) 2023 vincent d warmerdam 
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/koaning/uvtrick
Project-URL: Repository, https://github.com/koaning/uvtrick.git
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cloudpickle>=2.0.0
Requires-Dist: uv

<img src="img.png" width="125" height="125" align="right" />

### uvtrick

> A fun party trick, via `uv` and pickle, to run Python code from another venv ... into this one.

## Quickstart 

You can install this tool via: 

```
uv pip install uvtrick
```

## Usage 

### External scripts

There are a few ways to use this library. The first one is to use the `load` function to point 
to a Python script that contains the function you want to use. This function assumes that the 
script carries [inline script metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/). 

```python
from uvtrick import load

# Load the function `add` from the file `some_script.py`
# It runs in another virtualenv, but you get back the response via pickle. 
# Be aware of the limitations, please only consider base Python objects.
add = load("some_script.py", "add")

# This result is from the `some_script.py` file, running in another virtualenv 
# with `uv`. A pickle in a temporary file is used to communicate the result.
add(1, 2)  # 3
```

### From within Python

But you can also take it a step further and use the `Env` class to run a function in a specific environment. 

```python
from uvtrick import Env

# For illustration purposes, let's assume that rich is not part of the current environment. 
# Also note that all the imports happen inside of this function. 
def uses_rich():
    from rich import print
    from importlib import metadata

    version = metadata.version("rich")
    print(f"hello from rich=={version}")

# This runs the function `uses_rich` in a new environment with the `rich` package installed.
# Just like the `load` function before, the result is returned via pickle. 
Env("rich", python="3.12").run(uses_rich)
```

This approach is pretty useful if you are interested in running the same function in different versions of 
a dependency to spot a performance regression. You might be able to do that via something like:

```python
from uvtrick import Env

def uses_rich(a, b):
    from rich import print
    from importlib import metadata

    version = metadata.version("rich")
    print(f"hello from rich=={version}")
    return a + b

for version in (10, 11, 12, 13):
    Env(f"rich=={version}", python="3.12").run(uses_rich, a=1, b=2)
```

Be aware that a lot of pickling is happening under the hood here. This can be a problem if you are trying to pickle large objects
or if your function is returning an object that needs a dependency that is not installed in the environment that is calling `Env`. 

Also note that thusfar this entire project is merely the result of a very entertaining recreational programming session. 
We might want to gather some community feedback before suggesting production usage. 
