When I write a python script to run Devenv with the setting "Debug | Win32", it does nothing

Update: When I use subprocess.call instead of subprocess.Popen , the problem is solved - does anyone know what is the reason? And another problem came up: I can’t find a way to control the output ... Is there a way to redirect the output from subprocess.call to a string or something like that? Thanks!

I try to use Devenv to create projects, and it works fine when I enter it on the command line, for example devenv A.sln /build "Debug|Win32" , but when I use python to run it using Popen(cmd,shell=true) , where cmd is the same line as above, it shows nothing. If I remove | , change it only to "Debug" , it will work ...

Does anyone know why this is happening? I tried to put \ before | but still nothing happened.

This is the code I'm using:

 from subprocess import Popen, PIPE cmd = ' "C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv" solution.sln /build "Debug|Win32" ' sys.stdout.flush() p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE) lines = [] for line in p.stdout.readlines(): lines.append(line) out = string.join(lines) print out if out.strip(): print out.strip('\n') sys.stdout.flush() 

... which does not work, however, if I change Debug|Win32 to Debug , it works fine.

Thanks for every comment here.

+6
python subprocess
source share
5 answers

When shell = False , it treats the string as a single command, so you need to pass the / adrugments command to the list. Something like:

 from subprocess import Popen, PIPE cmd = [ r"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv", # in raw r"blah" string, you don't need to escape backslashes "solution.sln", "/build", "Debug|Win32" ] p = Popen(cmd, stdout=PIPE, stderr=PIPE) out = p.stdout.read() # reads full output into string, including line breaks print out 
0
source share

There is a difference between devenv.exe and devenv.com , both of which are executable and live in the same directory (sigh). The command lines used in the question, and some answers do not say what they want, so I'm not sure what will be used.

If you want to call from the command line, you need to make sure that you are using devenv.com , otherwise you will most likely get a graphical interface. I think this may be the cause of some (but not all) of the confusion.

+4
source share

See section 17.1.5.1. in the python documentation.

On Windows, Python automatically adds double quotes around the project configuration argument ie Debug | win32 is passed as "Debug | win32" to devenv. You DO NOT need to add double quotes, and you DO NOT need to pass shell = True to Popen.

Use ProcMon to view the argument string passed to devenv.

+1
source share

try double quoting: "devenv A.sln / build" Debug | Win32 "

0
source share

It looks like the Windows shell accepts this | as a channel (despite quotes and escape sequences). Have you tried shell=False instead?

0
source share

All Articles