When I write a python script to run Devenv with configure “Debug|Win32” it does nothing
-
20-08-2019 - |
Question
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
Solution
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
OTHER TIPS
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.
See section 17.1.5.1. in the python documentation.
On Windows, Python automatically adds the double quotes around the project configuration argument i.e Debug|win32 is passed as "Debug|win32" to devenv. You DON'T need to add the double quotes and you DON'T need to pass shell=True to Popen.
Use ProcMon to view the argument string passed to devenv.
try double quoting like: 'devenv A.sln /build "Debug|Win32"'
Looks like Windows' shell is taking that |
as a pipe (despite the quotes and escapes). Have you tried shell=False
instead?