uv icon indicating copy to clipboard operation
uv copied to clipboard

`uv python find --system` chooses Homebrew Python instead of system one

Open abitrolly opened this issue 1 year ago • 8 comments

Here is the Fedora 41 setup.

$ which uv
/home/linuxbrew/.linuxbrew/bin/uv
$ uv --version
uv 0.5.7 (Homebrew 2024-12-06)
$ which python
/usr/bin/python
$ which python3
/home/linuxbrew/.linuxbrew/bin/python3

Here are Python interpreters that uv sees.

$ uv python list     
cpython-3.13.1+freethreaded-linux-x86_64-gnu    <download available>
cpython-3.13.1-linux-x86_64-gnu                 <download available>
cpython-3.13.0-linux-x86_64-gnu                 /usr/bin/python3.13
cpython-3.13.0-linux-x86_64-gnu                 /usr/bin/python3 -> python3.13
cpython-3.13.0-linux-x86_64-gnu                 /usr/bin/python -> ./python3
cpython-3.13.0-linux-x86_64-gnu                 /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.13
cpython-3.12.8-linux-x86_64-gnu                 <download available>
cpython-3.12.7-linux-x86_64-gnu                 /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.12
cpython-3.11.11-linux-x86_64-gnu                <download available>
cpython-3.10.16-linux-x86_64-gnu                <download available>
cpython-3.9.21-linux-x86_64-gnu                 <download available>
cpython-3.8.20-linux-x86_64-gnu                 <download available>
cpython-3.7.9-linux-x86_64-gnu                  <download available>
pypy-3.10.14-linux-x86_64-gnu                   <download available>
pypy-3.9.19-linux-x86_64-gnu                    <download available>
pypy-3.8.16-linux-x86_64-gnu                    <download available>
pypy-3.7.13-linux-x86_64-gnu                    <download available>

I expect ux python find --system to get /usr/bin/python first (which points to /usr/bin/python3.13, but instead it chooses Homebrew one.

$ uv python find --system -vvv
    0.000121s DEBUG uv uv 0.5.7 (Homebrew 2024-12-06)
    0.002906s DEBUG uv_python::discovery Searching for default Python interpreter in managed installations or search path
    0.002969s DEBUG uv_python::discovery Searching for managed installations at `/home/anatoli/.local/share/uv/python`
    0.008152s DEBUG uv_python::discovery Found `cpython-3.13.0-linux-x86_64-gnu` at `/home/linuxbrew/.linuxbrew/bin/python3` (search path)
/home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.13

EDIT: This is important, because virtual environment with /usr/bin/python doesn't have problem with tkinter (https://github.com/astral-sh/uv/issues/7036) and Homebrew one needs extra steps.

abitrolly avatar Dec 08 '24 09:12 abitrolly

https://docs.astral.sh/uv/concepts/python-versions/#discovery-of-python-versions

A Python interpreter on the PATH as python, python3, or python3.x on macOS and Linux, or python.exe on Windows.

Adjusting the order in PATH might help your desired Python version be found first. While it's not the ideal solution, it should be able to fix your problem temporarily.

FishAlchemist avatar Dec 08 '24 10:12 FishAlchemist

Please see https://docs.astral.sh/uv/concepts/python-versions/#managed-and-system-python-installations

uv does not distinguish between Python versions installed by the operating system vs those installed and managed by other tools. For example, if a Python installation is managed with pyenv, it would still be considered a system Python version in uv.

We have some sort of unfortunate naming here that we inherited from pip install --system. I have some plans to re-frame the option, but uv does not have the capability to distinguish between a Python installation native to your system and one that you installed separately (at this time).

zanieb avatar Dec 08 '24 16:12 zanieb

I'd recommend being explicit about the interpreter you need in this case, e.g., --python /usr/bin/python3 python

zanieb avatar Dec 08 '24 16:12 zanieb

uv does not have the capability to distinguish between a Python installation native to your system and one that you installed separately (at this time)

On Linux system it is just a matter of sorting, putting /bin and /usr/bin first. In this particular case it helps if lookup for python was made first, and if it is not found, then a search for python3 would be done.

Even now the uv python list order is right, so selecting first available from it would do the trick. I tried to see if I can patch https://github.com/astral-sh/uv/blob/main/crates/uv-python/src/discovery.rs but nope, Rust is not my language.

abitrolly avatar Dec 09 '24 02:12 abitrolly

On Linux system it is just a matter of sorting, putting /bin and /usr/bin first.

We respect the order in PATH — which I believe is correct.

In this particular case it helps if lookup for python was made first, and if it is not found, then a search for python3 would be done.

Perhaps, but I think that would also be surprising? Some discussion on this in https://github.com/astral-sh/uv/issues/6444

zanieb avatar Dec 09 '24 15:12 zanieb

Even now the uv python list order is right, so selecting first available from it would do the trick.

The uv python list order isn't sorted by discovery order, it's sorted by implementation, version, then platform ~alphabetically.

zanieb avatar Dec 09 '24 15:12 zanieb

We have some sort of unfortunate naming here that we inherited from pip install --system.

Wait, that's not true. This is a uv-specific option:

❯ pip install --help | grep -- "--system" || echo "Nothing to see here."
Nothing to see here.
❯ uv pip install --help | grep -- "--system" || echo "Nothing to see here."
      --system                               Install packages into the system Python environment [env: UV_SYSTEM_PYTHON=]

pradyunsg avatar Dec 10 '24 13:12 pradyunsg

(I think we came up with --system on our own.)

charliermarsh avatar Dec 10 '24 13:12 charliermarsh

Oh, sorry — misremembered there. Thanks for the correction @pradyunsg.

The unfortunate naming is my mistake, I guess :)

zanieb avatar Dec 10 '24 14:12 zanieb

I think it might be mine :)

charliermarsh avatar Dec 10 '24 14:12 charliermarsh

It makes sense until you pick at the definition of a system interpreter.

Anyway.. this behavior is intentional. We should track better naming for the options elsewhere, but --system is here to stay for compatibility purposes.

zanieb avatar Dec 10 '24 14:12 zanieb

FYI, I suspect the name "system" was inspired by the actual pip flag "--break-system-packages".

notatallshaw avatar Dec 10 '24 16:12 notatallshaw

This root cause of this issue is actually a duplicate of https://github.com/astral-sh/uv/issues/6444

abitrolly avatar Dec 10 '24 17:12 abitrolly

Setting envvar UV_PYTHON_PREFERENCE=only-managed will make uv ignore system, homebrew and other unmanaged python installations in favour of the ones uv manages. This was the desired behaviour for my case.

Docs: https://docs.astral.sh/uv/configuration/environment/#uv_python_preference

ashleyconnor avatar Mar 07 '25 04:03 ashleyconnor