Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

python - Call a command in the VS developer command prompt programmatically

Introduction

I am trying to call a command ('nmake') that needs to be run from inside the VS Developer Command Prompt using Subprocess.Popen in python.

Setting the path for it won't work for my case.

I tried reading the docs and it didn't seem like there is a way to use something other than the default CMD.exe.

My code

import subprocess

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

Exception

It currently throws

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

so it's not actually using the executable provided there.

Notes

System used : Windows 10, python3.8

question from:https://stackoverflow.com/questions/65902953/call-a-command-in-the-vs-developer-command-prompt-programmatically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This probably happens in the cases in handling quotes in path filename. Few things which you can try:

  1. In popen argument add shell=True this causes subprocess to spawn an intermediate shell process, and tell it to run the command
  2. Use executable = [r"C:Program Files (x86)Microsoft Visual Studio2019BuildToolsCommon7ToolsVsMSBuildCmd.bat"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...