【问题标题】:Run BigQuery from Command Line Using Python Subprocess Module - FATAL Command 'bq ls redacted:dataid ' unknown使用 Python 子进程模块从命令行运行 BigQuery - 致命命令 'bq ls redacted:dataid' 未知
【发布时间】:2020-03-27 15:54:25
【问题描述】:

我需要使用 Python 的 Subprocess 模块通过 Google Cloud SDK 运行 BigQuery CLI 命令。当我从命令行运行以下命令时,事情会按我的预期工作:

我曾尝试在以下场景中运行此 bq 命令但没有成功:

  • 与 windows cmd.exe 文件的完整路径相结合
  • 与 bq.cmd 文件的完整路径同步
  • 第一次使用 bq 命令运行 bq.cmd 文件的完整路径后

这是我列出的第二个示例中的代码,因为我认为它最接近正确?

  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)

这是错误信息:

致命命令 'bq -q ls redacted:dataid' 未知
运行“bq.py help”以获取帮助

谢谢!

【问题讨论】:

  • 我没怎么用过powershell,但是有可能Python解释器对shell=True这块做坏事了?如果您不使用shell=True,会发生什么?

标签: python subprocess bq


【解决方案1】:

想通了,我需要分别列出命令的每个元素:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2014-12-22
    • 2020-07-25
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多