views:

341

answers:

4

Update: When I use the subprocess.call instead of subprocess.Popen, the problem is solved - does anybody know what's the cause? And there came another problem: I can't seem to 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'm trying to use Devenv to build projects, and it runs just fine when i type it in command prompt like devenv A.sln /build "Debug|Win32" - but when I use a python to run it using Popen(cmd,shell=true) where cmd is the same line as above, it shows nothing. If I remove the |, change it to "Debug" only, it works....

Does anybody know why this happens? I've tried putting a \ before |, but still nothing happened..

This is the code I am 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 doesn't work, however, if I swap Debug|Win32 with Debug, it works perfectly..

Thanks for every comment here

A: 

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

Martin P. Hellwig
I did double quoted actually..
A: 

Looks like Windows' shell is taking that | as a pipe (despite the quotes and escapes). Have you tried shell=False instead?

Alex Martelli
When I set shell to False, I can't seem to get any output so cannot make sure if it's working or not. Can you tell me how to get output when shell=False? Thanks
It's a mystery -- please show us the whole snippet of code, otherwise it will no doubt stay one;-).
Alex Martelli
It turned out it's the shell option caused the problem. Not sure why I wasn't able to get the output at first tho. Thanks!!
A: 

When shell = False is used, it will treat the string as a single command, so you need to pass the command/arugments as a 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
dbr
Actually the list of arguments doesn't work for me - when I run it, it keeps showing devenv's help dialog, which should mean only the first argument was called.However, you've got right sense with the shell argument - it turned out when I disable the shell = True, the original command line works out perfectly. There must be different meaning for | with shell option I guess.Thanks and cheers!
+2  A: 

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 don't say which they want so I'm not sure which will get used.

If you want to call from the command line then you need to ensure you use devenv.com, otherwise you're likely to get a GUI popping up. I think this might be the cause of some (but not all) of the confusion.

Scott Griffiths
this worked for me. I changed 'devenv' above to 'devenv.com' and it all started working.
gman