【问题标题】:python setup.py build_ext in Windows Azure pipelineWindows Azure 管道中的 python setup.py build_ext
【发布时间】:2021-04-09 01:27:48
【问题描述】:

我在这里阅读了很多问题/答案,但没有找到适合我的案例的解决方案。

我正在尝试对我的 SW (Python + C) 进行 Windows 测试。我在我的 GitHub 项目中使用 Azure 来测试它。 我需要 *.bat 文件的命令行解决方案。

我调用的批处理脚本

pip install --upgrade setuptools # required by VS
python setup.py build_ext --inplace

并收到错误:

error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

我尝试修改我的脚本如下:

pip install --upgrade setuptools
"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe" ^
  --add Microsoft.VisualStudio.Workload.NativeDesktop                            ^
  --includeOptional                                                              ^
  --includeRecommended                                                           ^
  --nocache                                                                      ^
  --quiet
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
python setup.py build_clib

但它看起来在执行过程中挂起,因为 VC 安装程序需要 GUI 答案。

有人可以给我一个建议,我应该在我的脚本中修改什么或提供一些解决方案的链接吗?

简而言之“如何在没有用户参与的情况下通过命令行安装'Microsoft C++ Build Tools'”?

谢谢

【问题讨论】:

标签: python windows azure msbuild visual-studio-2019


【解决方案1】:

好的,看来我放弃了在 Azure 下安装“VS Build Tools”。我没有找到任何可行的方法来做到这一点。

我做了什么:

第 1 部分。

解决我原来的问题(Windows 下的“python setup.py build_ext --inplace”)

只需要修改setup.py:

if sys.platform in ['win32', 'cygwin']:
    os.environ["DISTUTILS_USE_SDK"] = "1"

我查看了 setuptools 的源代码,发现它试图在 Windows 中“评估”构建环境。它试图找到许多不同的变量并失败了。设置此特定环境变量可防止此类内部逻辑,并使用“CC”等预设变量来获取编译器名称。没有深入研究这个逻辑,因为我已经失去了 ~ 一周的时间来推动简单的事情在 Windows 上工作。 (Python 减少了编程时间?啊哈……对于“hello world”应用程序。) 因此,在这种情况下无需安装“VS Build Tools”(关于 Python 中有价值的错误消息的问题)。

第 2 部分。

这些是我在 Azure 环境下安装“VS Build Tools”的失败尝试。也许它会在将来对某人有所帮助。

正在下载:

curl -o webimage.exe        ^
  --retry 5 --retry-delay 5 ^
  -L https://download.visualstudio.microsoft.com/download/pr/9b3476ff-6d0a-4ff8-956d-270147f21cd4/ccfb9355f4f753315455542f966025f96de734292d3908c8c3717e9685b709f0/vs_BuildTools.exe

我不确定链接本身。它今天在没有保证的情况下工作。

打印当前 VS 配置:

echo =============== VS config ===============
"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" ^
  export ^
  --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise" ^
  --config "%CD%\vs_config.txt" ^
  --passive
type "%CD%\vs_config.txt"
del "%CD%\vs_config.txt"

此配置在安装前后保持不变。也许我部分打印了它,但我厌倦了调查这些棘手的事情。组件集是模棱两可的,因为我没有机会让它工作并减少它。

vs_builttool.exe 安装

start /b /wait webimage.exe ^
    --add Microsoft.VisualStudio.Component.Roslyn.Compiler ^
    --add Microsoft.Component.MSBuild ^
    --add Microsoft.VisualStudio.Component.CoreBuildTools ^
    --add Microsoft.VisualStudio.Workload.MSBuildTools ^
    --add Microsoft.VisualStudio.Component.Windows10SDK ^
    --add Microsoft.VisualStudio.Component.VC.CoreBuildTools ^
    --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
    --add Microsoft.VisualStudio.Component.VC.Redist.14.Latest ^
    --add Microsoft.VisualStudio.Component.Windows10SDK.18362 ^
    --add Microsoft.VisualStudio.Component.VC.CMake.Project ^
    --add Microsoft.VisualStudio.Component.TestTools.BuildTools ^
    --add Microsoft.VisualStudio.Component.VC.ATL ^
    --add Microsoft.VisualStudio.Component.VC.ATLMFC ^
    --add Microsoft.Net.Component.4.8.SDK ^
    --add Microsoft.Net.Component.4.6.1.TargetingPack ^
    --add Microsoft.VisualStudio.Component.VC.CLI.Support ^
    --add Microsoft.VisualStudio.Component.VC.ASAN ^
    --add Microsoft.VisualStudio.Component.VC.Modules.x86.x64 ^
    --add Microsoft.VisualStudio.Component.TextTemplating ^
    --add Microsoft.VisualStudio.Component.VC.CoreIde ^
    --add Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core ^
    --add Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset ^
    --add Microsoft.VisualStudio.Component.VC.Llvm.Clang ^
    --add Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Llvm.Clang ^
    --add Microsoft.VisualStudio.Component.Windows10SDK.17763 ^
    --add Microsoft.VisualStudio.Component.Windows10SDK.17134 ^
    --add Microsoft.VisualStudio.Component.Windows10SDK.16299 ^
    --add Microsoft.VisualStudio.Component.VC.v141.x86.x64 ^
    --add Microsoft.Component.VC.Runtime.UCRTSDK ^
    --add Microsoft.VisualStudio.Component.VC.140 ^
    --add Microsoft.VisualStudio.Workload.VCTools ^
    --includeOptional --includeRecommended --nocache --wait --passive --quiet ^
    --installpath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise"

del webimage.exe

这里的重要选项是:

--wait

防止 shell 并行运行进程。在继续之前,您需要确保它完成。我不明白谁需要这种默认行为(默认 - 非阻塞 shell 命令执行)

--passive 

在良好的实用程序中类似于“--yes”。此选项无需用户交互。

我希望这篇文章会有所帮助,因为我之前没有找到并回答我原来的问题。

PS powershell 尝试:

Start-Process -Wait -FilePath  "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList "modify --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.Component.MSBuild --add Microsoft.VisualStudio.Component.Windows10SDK --add Microsoft.VisualStudio.Component.VC.CoreBuildTools --passive --norestart --installpath ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise"""

【讨论】:

  • 感谢您分享您的解决方法。我建议你可以标记自己的答案:)
  • 非常感谢!!我个人项目的 Windows 版本已损坏超过 1 年,现在我终于可以修复它了?
【解决方案2】:

你最好使用VS Build Tool,因为你只想使用vc build tool。 VS IDE 太大,可能会挂很长时间。 VS Build Tool 是轻量级的。

另外--includeOptional 将安装其他冗余组件,这将延长您的安装时间。在此过程中可能会导致一些意外问题。

此外,使用--wait。它将等待 Visual Studio 安装程序完成,然后再执行下一个命令。

1)下载https://visualstudio.microsoft.com/visual-cpp-build-tools/下的VS Build Tool

2) 使用这些命令:

xxx\vs_buildtools.exe --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --nocache --quiet --wait

另外,无论如何,Installer程序需要管理员权限才能运行,所以会出现提示框,所以不管你怎么做,都会出现提示,你必须点击它.

更新 1

感谢您的反馈,并知道您从 VM 中获得了一些权利。而且您似乎无法通过 GUI 从 Internet 轻松获取buildtool.exe

通过脚本获取buildtool.exe似乎有点复杂,所以使用vs_installer.exe更方便。

尝试vs_installer.exemodify 开关:

"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe" modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional"  --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended  --nocache --quiet

注意请以Administrator身份运行bat文件,否则会出现Administrator权限错误。

它会先执行修改,完成后会运行python文件。

【讨论】:

  • 看起来我没有仔细突出它 - 我没有任何 GUI 或对 azure 机器的控制。我需要通过脚本(* .bat)完成所有这些事情另外,据我了解,我们在 MSVS 安装程序中没有“等待”选项,我在 azure windows VM 中找不到“vs_buildtools.exe”(我必须下载它来自某个地方的脚本)
  • 通过您提供的链接,我在“vs_professional.exe”中看到了“等待”选项。我在 VS2019 安装中找不到它。我只看到“vs_installer.exe”、“vs_installer.windows.exe”和“vs_installershell.exe”文件。如果使用“--wait”,这些可执行文件会显示错误“没有这样的选项”
  • 感谢您的反馈和明确的信息。看来您的机器上有 VS2019 专业版。实际上,如果你的机器上有VS2019,你必须使用"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe" modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional" --add --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --nocache --quiet
  • 我已经更新了我的答案,你可以查看一下。
猜你喜欢
  • 2014-12-21
  • 2015-06-11
  • 2018-05-16
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
相关资源
最近更新 更多