【问题标题】:Call a command in the VS developer command prompt programmatically以编程方式在 VS 开发人员命令提示符中调用命令
【发布时间】:2023-03-18 20:04:01
【问题描述】:

简介

我正在尝试使用 python 中的Subprocess.Popen 调用需要从 VS 开发人员命令提示符内部运行的命令 ('nmake')。

为它设置路径不适用于我的情况。

我尝试阅读docs,但似乎没有其他方法可以使用默认 CMD.exe 之外的其他内容。

我的代码

import subprocess

command = ['nmake']
with subprocess.Popen(["nmake"],
                      executable=r"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsMSBuildCmd.bat",
                      stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                      universal_newlines=True) as process:
    print(process.stdout.readlines())
    print(process.stderr.readlines())

例外

目前抛出

'nmake' is not recognized as an internal or external command

所以它实际上并没有使用那里提供的可执行文件。

备注

使用系统:Windows 10,python3.8

【问题讨论】:

  • VS 开发者提示 is CMD.exe。但是,CMD 正在从 Visual Studio 安装运行该批处理文件以设置正确的路径。您可能想测试...\VsMSBuildCmd.bat & nmake,但我不确定VsMSBuildCmd.bat 的结果是否发生在nmake 启动之前。从逻辑上讲,它应该。
  • 很遗憾不起作用"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsMSBuildCmd.bat" & nmake 返回nmake 无法识别,然后运行我认为的蝙蝠。
  • 您可能想要制作自己的包装批处理文件。这允许您将这两个命令放在不同的行上,而 CMD 只需要调用包装器。
  • 为它设置路径对我的情况不起作用是什么意思。 你可以使用 sys.path 修改路径吗?你知道nmake.exe的位置吗?主要在 C 编译器的 bin 目录中。
  • 我确实知道nmake.exe的路径,但这本身是不够的,因为它依赖于.bat文件临时添加到路径中的许多其他库。

标签: python python-3.x visual-studio subprocess popen


【解决方案1】:

这可能发生在处理路径文件名中的引号的情况下。您可以尝试的几件事:

  1. popen 参数中添加shell=True 这会导致子进程产生一个中间shell 进程,并告诉它运行命令
  2. 使用executable = [r"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsMSBuildCmd.bat"]

【讨论】:

  • 可执行文件需要一个字符串而不是字符串列表。此外,传递 shell=True 并没有改变任何可悲的事情。我以前也尝试过,但没有运气。
【解决方案2】:

我终于(!!)设法用 cmake 完成了这种事情。 我整天都在为这件事苦苦挣扎,谷歌上没有任何东西能够以如此简洁的方式解决这个问题。

我使用 VS 开发人员命令提示符在 Windows 中为我的 CMake 项目运行带有 -GNinja 的 cmake,例如“cmake -GNinja”,但我需要使用 python 自动运行它,方法如下:

对于不使用 Python 运行,只需在常规 cmd.exe / powershell 中运行,您应该运行它,注意“&”链接命令:

C:\"Program Files (x86)"\"Microsoft Visual Studio"\2019\BuildTools\Common7\Tools\VsDevCmd.bat "&" cmake ..\src -GNinja

您可以将它放在一个 .bat 文件中以从某个脚本运行该文件,或者在 Python 中执行类似的操作:

os.chdir("C:\\git\\project\\build") # Changing to the build dir next to the project's src dir to build it with cmake.

os.system('C:\\"Program Files (x86)"\\"Microsoft Visual Studio"\\2019\\BuildTools\\Common7\\Tools\\VsDevCmd.bat' + "&" + 'cmake ..\\src -GNinja')  # Running cmake inside VS Developer CMD Prompt to get all the necessary libs and compilers automatically.

pycharm 的示例输出:

"C:\Program Files\Python38\python.exe" C:/git/automation/123.py
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.3
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
-- The C compiler identification is MSVC 19.29.30038.1
-- The CXX compiler identification is MSVC 19.29.30038.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx86/x86/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/git/project/build

Process finished with exit code 0

之后,如果你想使用忍者“make”来构建一些目标,也是一样的:

os.system('C:\\"Program Files (x86)"\\"Microsoft Visual Studio"\\2019\\BuildTools\\Common7\\Tools\\VsDevCmd.bat' + "&" + 'ninja <targetName>')

我假设你的 nmake 会是这样的:

os.system('C:\\"Program Files (x86)"\\"Microsoft Visual Studio"\\2019\\BuildTools\\Common7\\Tools\\VsDevCmd.bat' + "&" + 'nmake')

希望它能像我一样解决你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多