在vs代码中在powershell脚本中执行以下操作
& 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获取错误术语'pyenv‘不能识别为cmdlet、函数、脚本文件或可操作程序的名称。
它第一次起作用了,但现在不再起作用了,这很奇怪(可能是与缓存有关)。对做错了什么有什么想法吗?
终端输出
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发布于 2022-10-06 20:17:45
您正在修改注册表(值path在HKCU\Environment下),而不是修改当前进程($env:Path)的环境变量path。
阅读Environment.SetEnvironmentVariable Method (引用被截断,我强调的重要部分):
超载:
SetEnvironmentVariable(String, String)
创建、修改或删除当前进程中存储的环境变量。…
SetEnvironmentVariable(String, String, EnvironmentVariableTarget)
创建、修改或删除存储在当前进程或操作系统注册表项中的环境变量,该注册表项为当前用户或本地计算机保留。…
而且,您的SetEnvironmentVariable('path', …, "User")将值"$SomePath;"+"$env:Path"存储到注册表(用户路径)。这意味着用户路径值现在包含当前$env:Path的用户部分和系统部分。您的用户路径值会恶化,同时考虑到$env:Path是按照以下方式构造的:
# 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 ';')https://stackoverflow.com/questions/73974811
复制相似问题