Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find_uv_bin to find uv with system site packages #10195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ssbarnea
Copy link

@ssbarnea ssbarnea commented Dec 27, 2024

Summary

This allows uv to find its binary when it was installed as a system package. Current implementation fails if someone creates a venv with --system-packages because the relative uv path would be different.

Fixes: #10194

Test Plan

@ssbarnea ssbarnea marked this pull request as ready for review December 27, 2024 15:20
@ssbarnea
Copy link
Author

@charliermarsh please take a look at this and its bug. Let me know if further changes are needed as this is a blocker for addressing tox-dev/tox-uv#109 and in fact can happen easily even without tox. As soon you create a venv and install uv in it, once you try to create a new one, you may discover that you have an unusable uv package.

if os.path.isfile(target_path):
return target_path
# Search for `bin` when inside a venv created with use system-site-packages and uv is installed in the system.
pkg_root2 = os.path.dirname(os.path.dirname(os.path.dirname(pkg_root)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ../../..? What path is this targeting?

@zanieb
Copy link
Member

zanieb commented Dec 28, 2024

As soon you create a venv and install uv in it, once you try to create a new one, you may discover that you have an unusable uv package.

To be clear, this should not work by design

❯ uv venv one
Using CPython 3.13.1
Creating virtual environment at: one
Activate with: source one/bin/activate
❯ uv venv two
Using CPython 3.13.1
Creating virtual environment at: two
Activate with: source two/bin/activate
❯ uv pip install -p one uv
Using Python 3.13.1 environment at: one
Resolved 1 package in 181ms
Prepared 1 package in 1.12s
Installed 1 package in 7ms
 + uv==0.5.13
❯ ./one/bin/python -c "import uv; uv.find_uv_bin()"
❯ ./two/bin/python -c "import uv; uv.find_uv_bin()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import uv; uv.find_uv_bin()
    ^^^^^^^^^
ModuleNotFoundError: No module named 'uv'

Do you mean something else?

@zanieb zanieb self-assigned this Jan 6, 2025
@ssbarnea
Copy link
Author

To be clear, this should not work by design

Can you please elaborate around that? The way I read it is that you cannot make use of uv with venvs using system site packages.

uv venv --seed x1
source x1/bin/activate
pip install uv
python -m uv --version # works
uv --version # works

# case two:
uv venv --seed  --system-site-packages x2

source x2/bin/activate

pip install uv
Requirement already satisfied: uv in /Users/ssbarnea/.config/mise/installs/python/3.13.1/lib/python3.13/site-packages (0.5.18)
# ^ first problem, yes uv module is present due to system-site-packages but is not usable
# in any way, see below:

> python -m uv --version
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/ssbarnea/.config/mise/installs/python/3.13.1/lib/python3.13/site-packages/uv/__main__.py", line 47, in <module>
    _run()
    ~~~~^^
  File "/Users/ssbarnea/.config/mise/installs/python/3.13.1/lib/python3.13/site-packages/uv/__main__.py", line 27, in _run
    uv = os.fsdecode(find_uv_bin())
                     ~~~~~~~~~~~^^
  File "/Users/ssbarnea/.config/mise/installs/python/3.13.1/lib/python3.13/site-packages/uv/_find_uv.py", line 36, in find_uv_bin
    raise FileNotFoundError(path)
FileNotFoundError: /Users/ssbarnea/.local/bin/uv

# calling `uv` might give unreliable results now because clearly is not inside `x1/bin`
uv --version 

# example from my system:
❯ which -a uv
/Users/ssbarnea/.venv/bin/uv
/Users/ssbarnea/.config/mise/installs/python/3.13.1/bin/uv
/Users/ssbarnea/.config/mise/installs/python/3.12.8/bin/uv
/Users/ssbarnea/.config/mise/installs/python/3.11.11/bin/uv
/Users/ssbarnea/.config/mise/installs/python/3.10.16/bin/uv
/Users/ssbarnea/.config/mise/installs/python/3.9.21/bin/uv
/opt/homebrew/bin/uv

# expected behavior would be to use uv from within currently active venv (x1). The .venv one that is found instead is from user default

@zanieb
Copy link
Member

zanieb commented Jan 16, 2025

Can you please elaborate around that? The way I read it is that you cannot make use of uv with venvs using system site packages.

I think using system site packages seems okay if the virtual environment has opted into it. The part I'm saying should not work "by design" is that one virtual environment should not be able to access the uv of another virtual environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

find_uv_bin fails to find uv when already inside a virtualenv
2 participants