How to activate a conda environment within a PowerShell ScriptBlock

1.5k Views Asked by At

I'm looking to run/administer Miniconda remotely using PowerShell. On the remote PC, Miniconda3 was installed using:

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

Note the option /AddToPath=1 adds conda to the PATH environment variable.

On that remote computer, create a conda environment with PowerShell:

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

Cool, that worked:

PS C:\> Invoke-Command -ComputerName otherpc -ScriptBlock {conda env list}

# conda environments:
#
base                  *  C:\Users\user1\Miniconda3
py36                     C:\Users\user1\Miniconda3\envs\py36

However, the new environment cannot be simply activated:

PS C:\> Invoke-Command -ComputerName otherpc -ScriptBlock {
>>  conda activate py36
>>  conda info
>> }

     active environment : None
       user config file : C:\Users\user1\.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:\Users\user1\Miniconda3  (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:\Users\user1\Miniconda3\pkgs
                          C:\Users\user1\.conda\pkgs
                          C:\Users\user1\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\user1\Miniconda3\envs
                          C:\Users\user1\.conda\envs
                          C:\Users\user1\AppData\Local\conda\conda\envs
               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

I've tried adding conda init at the start of the ScriptBlock, and this only adds "no change" to various shell files, including a few *.ps1 files, but does not fix anything.

How should a conda environment be activated within a PowerShell ScriptBlock?

1

There are 1 best solutions below

0
On BEST ANSWER

The solution I found was to register a PowerShell session configuration with a start-up script that was previously configured by conda init:

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

This only needs to be done once, and is stored in the registry:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Plugin\UserProfile

Now use -ConfigurationName UserProfile with subsequent Invoke-Command, e.g.:

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