簡體   English   中英

當我編寫一個python腳本來運行配置“Debug | Win32”的Devenv時,它什么也沒做

[英]When I write a python script to run Devenv with configure “Debug|Win32” it does nothing

更新:當我使用subprocess.call而不是subprocess.Popen ,問題就解決了 - 有人知道是什么原因造成的嗎? 還有另一個問題:我似乎無法找到控制輸出的方法......有沒有辦法將輸出從subprocess.call重定向到字符串或類似的東西? 謝謝!

我正在嘗試使用Devenv來構建項目,當我在命令提示符下輸入它時運行得很好,比如devenv A.sln /build "Debug|Win32" - 但是當我使用python運行它時使用Popen(cmd,shell=true)其中cmd與上面的行相同,它什么也沒有顯示。 如果我刪除| ,只將它改為"Debug" ,它有效....

有人知道為什么會這樣嗎? 我試圖把一\\| ,但仍然沒有發生..

這是我正在使用的代碼:

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

...但是,如果我將Debug|Win32Debug交換,它可以正常工作...

感謝這里的每一條評論

devenv.exedevenv.com之間存在差異,兩者都是可執行的並且存在於同一目錄(嘆息)中。 問題中使用的命令行和一些答案沒有說出他們想要的,所以我不確定哪個會被使用。

如果你想從命令行調用,那么你需要確保你使用devenv.com ,否則你可能會彈出一個GUI。 我認為這可能是一些(但不是全部)混亂的原因。

見17.1.5.1節。 在python文檔中。

在Windows上,Python會自動在項目配置參數周圍添加雙引號,即Debug | win32作為“Debug | win32”傳遞給devenv。 您不需要添加雙引號,也不需要將shell = True傳遞給Popen。

使用ProcMon查看傳遞給devenv的參數字符串。

嘗試雙引用如:'devenv A.sln / build“Debug | Win32”'

看起來像Windows的shell正在接受那個| 作為管道(盡管有引號和逃逸)。 你試過shell=False嗎?

當使用shell = False ,它會將字符串視為單個命令,因此您需要將命令/ arugments作為列表傳遞..類似於:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM