Skip to main content

A fun party trick to run Python code from another venv into this one.

Project description

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.

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.

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:

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.

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

uvtrick-0.4.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

uvtrick-0.4.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file uvtrick-0.4.0.tar.gz.

File metadata

  • Download URL: uvtrick-0.4.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.1

File hashes

Hashes for uvtrick-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9dcca6df9c8d6024ec93cf2196206a419b5744f5cbac8172acb51554b66f5b4d
MD5 c8508b0ce2de480c8fb59eda91873be1
BLAKE2b-256 aa8b79d1c7bbb4512bcbac97f8b6f3ad904928f8902b00a6c0356ead2677ebf5

See more details on using hashes here.

File details

Details for the file uvtrick-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: uvtrick-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.1

File hashes

Hashes for uvtrick-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05dd52bc6eacf8ecb8ccab7e1612accf6b51510af339407c795ab35ffa03f631
MD5 b6b44239090ba277c07a08bdb3f80adc
BLAKE2b-256 36e86b5e3f9750a016cc1d0250d67679233845f40b0277253462c1dac7d7b43d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page