以编程方式在VS developer命令提示符中调用命令

2024-05-15 01:44:26 发布

您现在位置:Python中文网/ 问答频道 /正文

导言

我试图调用一个命令('nmake'),该命令需要使用python中的Subprocess.Popen从VS Developer命令提示符内部运行

为它设置路径对我的案例不起作用

我试着阅读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


Tags: 命令asstderrstdoutprocesscommandvssubprocess
2条回答

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

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

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

我在Windows中使用VS Developer命令提示符为我的cmake项目运行带有-GNinja的cmake,如“cmake-GNinja”,但我需要使用python自动运行该命令,如下所示:

要在没有Python的情况下运行,只需在常规cmd.exe/powershell中运行,您应该执行以下操作,请注意“&;”以链接命令:

C:\“程序文件(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

之后,如果你想使用忍者“制造”来建造一些目标,这是一样的:

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')

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

相关问题 更多 >

    热门问题