🔗: uvtrick
The following Python code executes the uses_rich
function in a fresh Python
virtual environment that's...
- managed by
uv
- using Python 3.12
- ensuring
rich
package is installed and available...even if it's not available in the current Python environment
from uvtrick import Env
def uses_rich():
from rich import print
print("hi :vampire:")
Env("rich", python="3.12").run(uses_rich)
The trick works by taking advantage of uv
's speed to perform the following operations, all within the Env.run()
method:
- Create a new temp directory
- Pickle the
args
andkwargs
, and save them topickled_inputs.pickle
- Use
inspect.getsource()
to get the source code of the function passed torun()
- Write that source to a
pytemp.py
file, along with the geneartedif __name__ == "__main__":
block responsible for calling the function with pickled inputs, and save its output to a new, separate pickle file namedtmp.pickle
Finally, after creating the new temporary Python file, it executes the program using a command similar to:
uv run --with rich --python 3.12 --quiet pytemp.py
Which reads the output from tmp.pickle
and returns it to the caller.
Sensational.