如何在PowerShell ScriptBlock中激活conda环境



我希望使用PowerShell远程运行/管理Miniconda。在远程电脑上,Miniconda3是使用安装的

Miniconda3-latest-Windows-x86_64.exe /InstallationType=JustMe /RegisterPython=1 /AddToPath=1 /S /D=%UserProfile%Miniconda3

请注意,选项/AddToPath=1conda添加到PATH环境变量中。

在该远程计算机上,使用PowerShell创建conda环境:

PS C:> Invoke-Command -ComputerName otherpc -ScriptBlock {conda create -n py36 python=3.6}

很酷,很管用:

PS C:> Invoke-Command -ComputerName otherpc -ScriptBlock {conda env list}
# conda environments:
#
base                  *  C:Usersuser1Miniconda3
py36                     C:Usersuser1Miniconda3envspy36

然而,新环境不能简单地激活:

PS C:> Invoke-Command -ComputerName otherpc -ScriptBlock {
>>  conda activate py36
>>  conda info
>> }
active environment : None
user config file : C:Usersuser1.condarc
populated config files :
conda version : 4.8.5
conda-build version : not installed
python version : 3.8.5.final.0
virtual packages :
base environment : C:Usersuser1Miniconda3  (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:Usersuser1Miniconda3pkgs
C:Usersuser1.condapkgs
C:Usersuser1AppDataLocalcondacondapkgs
envs directories : C:Usersuser1Miniconda3envs
C:Usersuser1.condaenvs
C:Usersuser1AppDataLocalcondacondaenvs
platform : win-64
user-agent : conda/4.8.5 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.17763
administrator : True
netrc file : None
offline mode : False

我尝试在ScriptBlock的开头添加conda init,这只会添加";没有变化";到各种shell文件,包括一些*.ps1文件,但不会修复任何内容。

应该如何在PowerShell ScriptBlock中激活conda环境?

我找到的解决方案是使用conda init:之前配置的启动脚本注册PowerShell会话配置

Invoke-Command -ComputerName otherpc -ScriptBlock {
Register-PSSessionConfiguration -Name UserProfile -StartupScript %USERPROFILE%DocumentsWindowsPowerShellprofile.ps1
}
# restart afterward
Get-Service -ComputerName otherpc -Name WinRM | Restart-Service

这只需要完成一次,并存储在注册表中:

ComputerHKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionWSMANPluginUserProfile

现在将-ConfigurationName UserProfile与后续Invoke-Command一起使用,例如:

Invoke-Command -ComputerName otherpc -ConfigurationName UserProfile -ScriptBlock {
conda activate py36
conda info
}

相关内容

  • 没有找到相关文章

最新更新