Search code examples
pythonwindowspyenv

pyenv windows the term 'pyenv' is not recognized as the name of a cmdlet, function


Doing the following in a powershell script in vs code

& pip install pyenv-win --target "$HOME\.pyenv"
[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('path', $HOME + "\.pyenv\pyenv-win\bin;" + $HOME + "\.pyenv\pyenv-win\shims;" + $env:Path,"User")
& pyenv --version

getting the error The term 'pyenv' is not recognized as the name of a cmdlet, function, script file, or operable program.

the first time it worked but no longer does which is weird (maybe something to do with cache). any ideas on what am doing wrong?

output from the terminal

collecting pyenv-win
  Using cached pyenv_win-3.1.1-py3-none-any.whl (3.6 MB)
Installing collected packages: pyenv-win
Successfully installed pyenv-win-3.1.1
WARNING: Target directory C:\Users\name\.pyenv\.version already exists. Specify --upgrade to force replacement.
WARNING: Target directory C:\Users\name\.pyenv\pyenv-win already exists. Specify --upgrade to force replacement.
WARNING: Target directory C:\Users\name\.pyenv\pyenv_win-3.1.1.dist-info already exists. Specify --upgrade to force replacement.
& : The term 'pyenv' 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 C:\Users\name\Source\Repos\LoadData\install_pyenv.ps1:6 char:3
+ & pyenv --version
+   ~~~~~
    + CategoryInfo          : ObjectNotFound: (pyenv:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Solution

  • You are modifying registry (value path under HKCU\Environment) rather than environment variable path for the current process ($env:Path).

    Read Environment.SetEnvironmentVariable Method (citations truncated, important parts emphasized by me):

    Overloads:

    SetEnvironmentVariable(String, String)

    Creates, modifies, or deletes an environment variable stored in the current process. …

    SetEnvironmentVariable(String, String, EnvironmentVariableTarget)

    Creates, modifies, or deletes an environment variable stored in the current process or in the Windows operating system registry key reserved for the current user or local machine. …

    Moreover, your SetEnvironmentVariable('path', …, "User") stores value "$SomePath;"+"$env:Path" to the registry (user path). This means that user path value now contains both user and system parts of the current $env:Path. Your user path value is deteriorated having in mind that $env:Path is constructed as follows:

    # if auxiliary variables `$systPath` and `$userPath` are as follows
    $systPath = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\').GetValue('Path').TrimEnd(';')
    $userPath = (Get-Item 'HKCU:\Environment\').GetValue('Path').TrimEnd(';')
    # then
    $env:path -eq ($systPath, $userPath -join ';')