Skip to main content
 首页 » 编程设计

python之Powershell 无法将 conda 识别为 cmdlet、函数或可运行程序

2025年01月19日8mate10pro

我的新笔记本电脑出现这个问题已经有几个小时了,但我无法弄清楚是什么原因造成的。我正在尝试使用 conda 安装 scikit-learn 并收到以下错误

conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 

名称的拼写,或者如果包含路径,请验证路径是否正确并重试。 在行:1 字符:1 + conda 安装-c anaconda scikit-learn + ~~~~~ + CategoryInfo:ObjectNotFound:(conda:String)[],CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

有没有其他人在 Windows 10 上遇到过类似问题?

请您参考如下方法:

您是否已为此用例激活环境?

关于各种 Windows 7 及更高版本的 conda 故障的 GitHub conda 讨论中有一个长期运行的线程,此处:

https://github.com/conda/conda/issues/626

一个建议是:

The down and dirty:

  1. Check to see if activate works in cmd.exe.

  2. If doesn't work or not acceptable--as @TurboTim shows:

    Powershell needs the path to each env (anaconda3\envs\someenv\py33.exe. Laborious! :p

    If you don't mind polluting your powershell a little, you can create a profile script which is run every time you open powershell. The below will add the functions Invoke-CmdScript, Conda-Activate, Conda-Deactivate to your powershell. See Tim's link above for why.

    PS C:> New-Item -Path $profile -ItemType File -Force

This creates a script at:

PS C:\> echo $profile 

...something like C:\Users\yourUser\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Edit that script.

PS C:\> explorer $profile 

Add this code, save, and reopen powershell (or . $profile ) :

function Invoke-CmdScript { 
  param( 
    [String] $scriptName 
  ) 
  $cmdLine = """$scriptName"" $args & set" 
  & $Env:SystemRoot\system32\cmd.exe /c $cmdLine | 
  Select-String '^([^=]*)=(.*)$' | ForEach-Object { 
    $varName = $_.Matches[0].Groups[1].Value 
    $varValue = $_.Matches[0].Groups[2].Value 
    Set-Item Env:$varName $varValue 
  } 
} 
$condaRoot = "$Env:USERPROFILE\Anaconda3" 
function Conda-Activate([string]$condaEnv) {Invoke-CmdScript $condaRoot\Scripts\activate.bat $condaEnv} 
function Conda-Deactivate {Invoke-CmdScript $condaRoot\Scripts\deactivate.bat} 
 
 
Usage: 
C:\> Conda-Activate TFTheano 
C:\> Conda-Activate root 
C:\> conda info --envs 

Disclaimers: Deactivate, as written, didn't do the job for me, thus I just use Conda-Activate to move around. Also, I don't suspect there are security cautions with adding Invoke-Cmd to profile, so please chime in if 2 cents