uv
uv copied to clipboard
Start search for projects and virtual environments at script directory (instead of cwd)
Question
Consider a project with the following folder structure:
.
├── foo
│ ├── hello.py
│ └── pyproject.toml
└── pyproject.toml
pyproject.toml
[project]
name = "uv-nested"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []
[tool.uv.workspace]
members = ["foo"]
foo/pyproject.toml
[project]
name = "foo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = ["rich"]
foo/hello.py
from rich import print
def main():
print("Hello from foo!")
if __name__ == "__main__":
main()
-
Now try to run
foo/hello.py:❯ uv run foo/hello.py Using CPython 3.11.11 Creating virtual environment at: .venv Traceback (most recent call last): File "/Users/jedumas/sandbox/uv_nested/foo/hello.py", line 1, in <module> from rich import print ModuleNotFoundError: No module named 'rich' -
If you cd in the subfolder, it runs correctly:
❯ pushd foo; uv run foo/hello.py; popd Hello from foo! -
And now that we have a
foo/.venv, callinguv runfrom the root folder will work!❯ uv run foo/hello.py Hello from foo!
So my question is: is this expected behavior? I would expect that when calling uv run on a script, uv would use the most nested pyproject.toml to setup the venv and execute a given script.
Platform
macOS 14.5
Version
uv 5.29.0