astral-sh / uv

An extremely fast Python package installer and resolver, written in Rust.
https://astral.sh/
Apache License 2.0
15.44k stars 452 forks source link

Bug: `uv venv` on windows cannot find python from pyenv #1660

Closed Warchant closed 5 months ago

Warchant commented 5 months ago
(latest windows 11)
$ pyenv version
* 3.10.8 (set by C:\Users\warch\.pyenv\pyenv-win\version)
$ python --version
Python 3.10.8
$ pipx install uv
$ uv --version
uv 0.1.4

On Windows I use pyenv to manage python versions. Currently 3.10.8 is installed. And I never installed python via downloadable installer.

On clean windows py and python point to a shim that Windows created at C:\Users\warch\AppData\Local\Microsoft\WindowsApps\python.exe - that thing is not a real python - if you run python or py Windows opens Microsoft Store, and will propose to install python from the Store.

Bug description:

$ uv venv venv2
  x failed to canonicalize path `C:\Users\warch\AppData\Local\Microsoft\WindowsApps\python.exe`
  `-> The file cannot be accessed by the system. (os error 1920)
$ rm C:\Users\warch\AppData\Local\Microsoft\WindowsApps\python.exe
$ uv venv venv2
  x Failed to run `py --list-paths` to find Python installations. Is Python installed?
  `-> program not found
$ py
py : The term 'py' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At line:1 char:1
+ py
+ ~~
    + CategoryInfo          : ObjectNotFound: (py:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

$ uv venv venv2 -p python
  x Querying Python at `C:\Users\warch\.pyenv\pyenv-win\shims\python.bat` failed with status exit code: 1:
  | --- stdout:

  | --- stderr:
  | File "<string>", line 1
  |     import json ||  goto :error
  |                 ^
  | SyntaxError: invalid syntax
  | ---

On Windows uv venv uses py --list-versions to get python, and I think uv could do more checks to ensure py exists, to check if pyenv is installed, etc:

(very rough python implementation of possible checks):

def get_python_path_windows():
  # try to use `py` to find `python`
  py = shutil.which("py")
  if py and check_if_valid_win32_app(py):
    return get_python_from_py(py)

  # maybe `python` exists in PATH?
  python = shutil.which("python")
  if python and check_if_valid_win32_app(python):
    # check_if_valid_win32_app would return False on `python.bat` created by pyenv
    return python

  # check if pyenv is installed
  pyenv = shutil.which("pyenv")
  if pyenv:
    return get_python_from_pyenv(pyenv)

  return None

def get_python_from_pyenv(pyenv):
  # pyenv which python returns path to python.exe
  return subprocess.check_output([pyenv, "which", "python"]).strip().decode("utf-8")
zanieb commented 5 months ago

Thanks for the report. We have some bugs around our handling of pyenv shims right now. Windows is especially complicated for Python version discovery :(

ofek commented 5 months ago

https://github.com/astral-sh/uv/issues/1310