[英]Run BigQuery from Command Line Using Python Subprocess Module - FATAL Command 'bq ls redacted:dataid ' unknown
I need to use Python's Subprocess module to run BigQuery CLI commands via the Google Cloud SDK.我需要使用 Python 的 Subprocess 模块通过 Google Cloud SDK 运行 BigQuery CLI 命令。 When I run the below command from the command line things work as I expect:
当我从命令行运行以下命令时,事情按我的预期工作:
I have tried running this bq command in the below scenarios with no success:我曾尝试在以下场景中运行此 bq 命令,但没有成功:
Here is the code from the second example I listed because I think it's closest to being correct?这是我列出的第二个示例中的代码,因为我认为它最接近正确?
list_tables_bigquery = 'bq ls redacted:dataid'
try:
process = subprocess.Popen(["C:\\Users\\redacted.user\\AppData\\Local\\Google\Cloud SDK\\google-cloud-sdk\\bin\\bq.cmd",list_tables_bigquery],stdout=subprocess.PIPE, stderr = subprocess.STDOUT,shell=True,universal_newlines=True)
except Exception as error:
print(error)
powershell_communication = process.communicate()[0]
print(powershell_communication)
Here is the error message:这是错误消息:
FATAL Command 'bq -q ls redacted:dataid' unknown
致命命令 'bq -q ls redacted:dataid' 未知
Run 'bq.py help' to get help运行“bq.py help”获取帮助
Thanks!谢谢!
figured it out, I needed to list each element of my command separately:想通了,我需要分别列出我的命令的每个元素:
try:
#open bq program
process = subprocess.Popen(["C:\\Users\\redacted.user\\AppData\\Local\\Google\Cloud SDK\\google-cloud-sdk\\bin\\bq.cmd",'ls', 'redacted:id'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True,universal_newlines=True)
except Exception as error:
print(error)
bq_communication = process.communicate()[0]
print(powershell_communication)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.