uv icon indicating copy to clipboard operation
uv copied to clipboard

Start search for projects and virtual environments at script directory (instead of cwd)

Open jdumas opened this issue 8 months ago • 12 comments

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()
  1. 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'
    
  2. If you cd in the subfolder, it runs correctly:

    ❯ pushd foo; uv run foo/hello.py; popd
    Hello from foo!
    
  3. And now that we have a foo/.venv, calling uv run from 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

jdumas avatar Feb 07 '25 00:02 jdumas