2015-02-11 181 views
2

這可能很簡單,但相當長一段時間我收到這個錯誤!Windows上的Bigquery命令行工具

在Windows和Python 2.7版

import subprocess

p = subprocess.Popen(["C:\Program Files\Google\Cloud SDK\google-cloud-sdk\bin\bq"])

回溯(最近通話最後一個): 「在 文件,第1行,」 C 文件 「:\ Python27 \ LIB \ subprocess.py」,行710在初始化 errread,ERRWRITE) 文件 「C:\ Python27 \ lib中\ subprocess.py」,線路958,在_execute_child STARTUPINFO) WindowsError:[錯誤2]系統無法找到該文件指定

當我通過bq命令提示正常運行時,它運行完美。 我缺少一些與子進程和bq。

感謝

編輯: 下面我提供了一些解決方案,嘗試後發現,當我使用「shell =真」大多數命令的工作在Windows外殼!

e.g: p = subprocess.Popen('dir', shell=True)

回答

2

錯誤消息說,它無法找到您指定的可執行文件。這很可能是由於使用反斜槓造成的(\)。反斜槓在Python字符串中轉義字符。您可以用\\/替換\。此外,你應該添加文件擴展名.cmd。在最後.exe:試試這個:

p = subprocess.Popen(["C:/Program Files/Google/Cloud SDK/google-cloud-sdk/bin/bq.cmd"]) 
2

您需要提供全名包括文件擴展名例如,

from subprocess import Popen 

p = Popen(r"C:\Program Files\Google\Cloud SDK\google-cloud-sdk\bin\bq.exe") 

通知。

r'' - 原始字符串文字用於避免在Windows路徑中轉義反斜槓。

注意:如果r"C:\Program Files\Google\Cloud SDK\google-cloud-sdk\bin%PATH%那麼您可以使用p = Popen("bq")。見Popen with conflicting executable/path

注:如果實際bq文件名與所使用的Popen()不會找到它,除非你明確指定的文件擴展名.cmdas @Antonio suggests然後(as said in the linkCreateProcess()的Windows函數結束。如果你使用shell(cmd.exe);它使用不同的規則(例如枚舉%PATHEXT%as said in the link),因此即使僅給出bq(假設.cmd%PATHEXT%中),也可能找到bq.cmd

After trying with several solutions provided below I found that when I use "shell=True" most of the commands work on windows shell!

dir是一個內部shell命令。除非有其他一些dir程序;如果沒有shell=True,則在Windows上啓動cmd.exe%COMSPEC%)將無法工作。某些命令與shell=True一起工作的原因並且在沒有它的情況下似乎失敗是由於用於查找可執行文件的規則與shell=False大小寫(缺省值)的區別。這些規則列舉in the link (follow it and read it, I'll wait. It is 4th time, the link is mentioned in the answer)

2

使用下列操作之一:在谷歌雲SDK

from subprocess import Popen 
p = Popen(["C:/Program Files/Google/Cloud SDK/google-cloud-sdk/bin/bq.cmd"]) 
p = Popen([r"C:\Program Files\Google\Cloud SDK\google-cloud-sdk\bin\bq.cmd"]) 

BQ可執行窗戶被稱爲bq.cmd。當從命令行調用它時,cmd會自動查找.cmd擴展名,而python解釋器不會。

+0

'Popen()'使用Windows API('CreateProcess()')來運行子進程。它是'cmd.exe',它具有不同的規則,而不是Python解釋器。 – jfs 2015-02-19 02:15:17