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

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

Closed
Warchant opened this issue Feb 18, 2024 · 2 comments · Fixed by #1711
Closed

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

Warchant opened this issue Feb 18, 2024 · 2 comments · Fixed by #1711
Labels
bug Something isn't working virtualenv Related to virtual environments windows Specific to the Windows platform

Comments

@Warchant
Copy link

Warchant commented Feb 18, 2024

(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 zanieb added virtualenv Related to virtual environments bug Something isn't working labels Feb 18, 2024
@zanieb
Copy link
Member

zanieb commented Feb 18, 2024

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

@zanieb zanieb added the windows Specific to the Windows platform label Feb 18, 2024
@ofek
Copy link
Contributor

ofek commented Feb 18, 2024

#1310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working virtualenv Related to virtual environments windows Specific to the Windows platform
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants