astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
23.33k stars 669 forks source link

uv can't find python interpreter automatically #5589

Closed newne closed 2 months ago

newne commented 2 months ago

Here is the problem:

(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ uv --version
uv 0.2.31
(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ uv python list
warning: `uv python list` is experimental and may change without warning
cpython-3.12.4-linux-x86_64-gnu     <download available>
cpython-3.12.3-linux-x86_64-gnu     /usr/bin/python3.12
cpython-3.12.3-linux-x86_64-gnu     /usr/bin/python3 -> python3.12
cpython-3.12.3-linux-x86_64-gnu     /bin/python3.12
cpython-3.12.3-linux-x86_64-gnu     /bin/python3 -> python3.12
cpython-3.11.9-linux-x86_64-gnu     <download available>
cpython-3.10.14-linux-x86_64-gnu    /home/ncg/.local/share/uv/python/cpython-3.10.14-linux-x86_64-gnu/bin/python3 -> python3.10
cpython-3.9.19-linux-x86_64-gnu     <download available>
cpython-3.9.12-linux-x86_64-gnu     /home/ncg/anaconda3/bin/python3.9
cpython-3.9.12-linux-x86_64-gnu     /home/ncg/anaconda3/bin/python3 -> python3.9
cpython-3.9.12-linux-x86_64-gnu     /home/ncg/anaconda3/bin/python -> python3.9
cpython-3.8.19-linux-x86_64-gnu     <download available>
pypy-3.7.13-linux-x86_64-gnu        <download available>
(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ uv venv -p 3.10
  × No interpreter found for Python 3.10 in system path
(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ uv python install 3.10
warning: `uv python install` is experimental and may change without warning
Searching for Python versions matching: Python 3.10
Found existing installation for Python 3.10: cpython-3.10.14-linux-x86_64-gnu
(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ uv venv --python /home/ncg/.local/share/uv/python/cpython-3.10.14-linux-x86_64-gnu/bin/python3 
Using Python 3.10.14 interpreter at: /home/ncg/.local/share/uv/python/cpython-3.10.14-linux-x86_64-gnu/bin/python3
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
(base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ source .venv/bin/activate
(offline_rl_control) (base) ncg@ncg-pc:~/Documents/code/leap-algorithm/production/cooler/offline_rl_control$ python -V
FishAlchemist commented 2 months ago

As for the solution to this problem, adding python-preference and setting its value to something other than only-system should do the trick. e.g.

uv venv -p 3.10.14 --python-preference installed

Although I tested this on Windows, I used the same solution to resolve the issue of not being able to create a virtual environment using managed installation on Ubuntu.

python-preference possible values for uv 0.2.31 (48162de97 2024-07-29)

--python-preference <PYTHON_PREFERENCE>
          Whether to prefer using Python installations that are already present on the system, or those that are downloaded and installed by uv

          Possible values:
          - only-managed: Only use managed Python installations; never use system Python installations
          - installed:    Prefer installed Python installations, only download managed Python installations if no system Python installation is found
          - managed:      Prefer managed Python installations over system Python installations, even if fetching is required
          - system:       Prefer system Python installations over managed Python installations
          - only-system:  Only use system Python installations; never use managed Python installations

According to the document, the default value of python-preference should be installed. However, the actual result seems to be inconsistent with the documentation. https://github.com/astral-sh/uv/blob/c0d3da8b6ae134036b3ed024762b08c4fb9f967b/docs/settings.md#python-preference--python-preference- For more information about this flag, you might need to ask the programmer who wrote UV. I've only submitted document modifications.

python-preference options test

❯ uv python list --only-installed
warning: `uv python list` is experimental and may change without warning
cpython-3.12.4-windows-x86_64-none     C:\Users\user_name\AppData\Local\Programs\Python\Python312\python.exe
cpython-3.11.9-windows-x86_64-none     C:\Users\user_name\AppData\Local\Programs\Python\Python311\python.exe
cpython-3.10.14-windows-x86_64-none    C:\Users\user_name\AppData\Roaming\uv\data\python\cpython-3.10.14-windows-x86_64-none\install\python.exe
cpython-3.10.11-windows-x86_64-none    C:\Users\user_name\AppData\Local\Programs\Python\Python310\python.exe
cpython-3.9.19-windows-x86_64-none     C:\Users\user_name\AppData\Roaming\uv\data\python\cpython-3.9.19-windows-x86_64-none\python.exe
# ----------------------------------------
❯ uv venv -p 3.10.14 --python-preference only-managed
Using Python 3.10.14
Creating virtualenv at: .venv
Activate with: .venv\Scripts\activate
# ----------------------------------------
❯ uv venv -p 3.10.14 --python-preference installed
Using Python 3.10.14
Creating virtualenv at: .venv
Activate with: .venv\Scripts\activate
# ----------------------------------------
❯ uv venv -p 3.10.14 --python-preference managed
Using Python 3.10.14
Creating virtualenv at: .venv
Activate with: .venv\Scripts\activate
# ----------------------------------------
❯ uv venv -p 3.10.14 --python-preference system
Using Python 3.10.14
Creating virtualenv at: .venv
Activate with: .venv\Scripts\activate
# ----------------------------------------
❯ uv venv -p 3.10.14 --python-preference only-system
  × No interpreter found for Python 3.10.14 in system path or `py` launcher
# ----------------------------------------
❯ uv venv -p 3.10.14
  × No interpreter found for Python 3.10.14 in system path or `py` launcher
charliermarsh commented 2 months ago

\cc @zanieb @konstin

zanieb commented 2 months ago

Using managed interpreters with uv venv requires the --preview flag right now. We'll change this soon to match the behavior of all the preview commands (like uv python install).

zanieb commented 2 months ago

This should be resolved now.