ContinuumIO / anaconda-issues

Anaconda issue tracking
648 stars 223 forks source link

vs2017_win-64 not working with PowerShell #10551

Open swaldhoer opened 5 years ago

swaldhoer commented 5 years ago

Actual Behavior

The Anaconda package vs2017_win-64 basically runs the vcvars64.bat script coming with Visual Studio 2017 to have an activated conda environment with all environment variables set for using the MSVC compiler inside that activated environment. This package works when running <InstallDirectory>\Scripts\activate.bat in cmd.

Starting with conda 4.6.0 PowerShell support was added to condas environment activation. Running etenv in PowerShell works well and the environment gets activated, but the environment variables for MSVC are not set (and that is obvious as this package works with a batch file).

I would suggest this solution from Visual Studios vswhere.exe to make the package also working with PowerShell (see vswherewiki):

# see https://github.com/Microsoft/vswhere/wiki/Start-Developer-Command-Prompt#using-powershell
$installationPath = vswhere.exe -prerelease -latest -property installationPath
if ($installationPath -and (test-path "$installationPath\Common7\Tools\vsdevcmd.bat")) {
  & "${env:COMSPEC}" /s /c "`"$installationPath\Common7\Tools\vsdevcmd.bat`" -no_logo && set" | foreach-object {
    $name, $value = $_ -split '=', 2
    set-content env:\"$name" $value
  }
}

vswhere comes starting form VS2017, so maybe for older versions of VS this needs to be changed a little bit.

Expected Behavior

The environment variables should be set correctly for PowerShell after etenv is run as they are set correctly in cmd after activate.bat is run.

Steps to Reproduce

PowerShell cmd
activate step PS C:> etenv C:>C:\Anaconda3\Scripts\activate
activating activating, except for MSVC C:>SET DISTUTILS_USE_SDK=1
C:>SET MSSdk=1
C:>SET "VS_VERSION=15.0"
C:>SET "VS_MAJOR=15"
C:>SET "VS_YEAR=2017"
C:>set "MSYS2_ARG_CONV_EXCL=/AI;/AL;/OUT;/out"
C:>set "MSYS2_ENV_CONV_EXCL=CL"
C:>set "PY_VCRUNTIME_REDIST=\bin\vcruntime140.dll"
C:>set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\"
C:>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\" )
C:>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\" )
C:>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\" )
C:>SET "CMAKE_GENERATOR=Visual Studio 15 2017 Win64"
C:>pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\"
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>CALL "VC\Auxiliary\Build\vcvars64.bat"
**
Visual Studio 2017 Developer Command Prompt v15.0.26730.16
Copyright (c) 2017 Microsoft Corporation
**
[vcvarsall.bat] Environment initialized for: 'x64'
done (base) PS C:> (base) C:>
Operating System:

Windows

conda info
(base) C:\>conda info

     active environment : base
    active env location : C:\Anaconda3
            shell level : 1
       user config file : C:\Users\development\.condarc
 populated config files :
          conda version : 4.6.1
    conda-build version : 3.17.6
         python version : 3.6.7.final.0
       base environment : C:\Anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/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:\Anaconda3\pkgs
                          C:\Users\development\.conda\pkgs
                          C:\Users\development\AppData\Local\conda\conda\pkgs
       envs directories : C:\Anaconda3\envs
                          C:\Users\development\.conda\envs
                          C:\Users\development\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.6.1 requests/2.21.0 CPython/3.6.7 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False
swaldhoer commented 5 years ago

Starting with Visual Studio 2019 version 16.2 there is an PowerShell wrapper for Visual Studio (Developer PowerShell for VS 2019), see devblogs.microsoft.com.

To activate the Developer PowerShell you need to run the following command:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command "& { Import-Module C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll; Enter-VsDevShell -InstanceId YOUR_INSTANCE_ID}"

And the instance ID can be found by using vswhere: E.g. to find the instanceId for VS 2017:

"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property instanceId -version [15.0,16.0)

or VS 2019:

"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property instanceId -version [16.0,17)

Therefore you just need to get the instanceId and then pass it to Enter-VsDevShell and you could use PowerShell, conda PowerShell wrapper and Visual Studio.