uv icon indicating copy to clipboard operation
uv copied to clipboard

Should `uv run` always create a virtual environment?

Open zanieb opened this issue 1 year ago • 7 comments

I think yes, if one cannot be found. Otherwise, a command can mutate the system environment (e.g. uv run pip install anyio).

zanieb avatar Aug 06 '24 22:08 zanieb

Interesting, so like, never run in the base env?

charliermarsh avatar Aug 06 '24 22:08 charliermarsh

Maybe? It seems okay to run in virtual or project environments, but weird for non-virtual environments.

zanieb avatar Aug 06 '24 22:08 zanieb

I feel like uv creates virtual environments so quickly that it might not be a big deal to always create a virtual environment.

Does that seem like a good default behavior? Lately the python dev community has shifted in the direction of isolating as much as possible, whenever possible.

chrisrodrigue avatar Aug 07 '24 21:08 chrisrodrigue

Not entirely sure about this. I see it as whether or not the base env (I call it global env) is still necessary.

uv tool[^1] has remove a lot of the need for base env, but I think there are a few key use cases that pipx historically do not support.

A motivating example would be Jupyter where many users treat it as a global tool not belonging to any particular environment. How does uv run jupyter work here?

[^1]: huh, I just learned about uvx. What's the difference? Both seems like pipx replacement.

ketozhang avatar Aug 22 '24 02:08 ketozhang

uv run jupyter will fail unless you're in a project that requires jupyter or it is otherwise installed on your system, e.g., in the environment of the interpreter that uv finds. uvx jupyter will install and run jupyter in an isolated environment. There's some documentation on this.

We can't really get around the base environment. It can be immutable, but we're a Python package tool and we need a Python interpreter for most operations. uv run will probably always find a Python interpreter. The question is if we should force you to be isolated from that base environment by creating an empty virtual environment.

zanieb avatar Aug 22 '24 03:08 zanieb

Sounds good to me.

Is the venv created at some temporary directory (current behavior for uv run example.py)?

ketozhang avatar Aug 23 '24 01:08 ketozhang

That would be the idea, yeah. Though for uv run example.py I think we'll re-use a cached environment.

zanieb avatar Aug 23 '24 12:08 zanieb