简体   繁体   中英

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. 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:

  • in tandem with the full path to the windows cmd.exe file
  • in tandem with the full path to the bq.cmd file
  • after first running the full path to the bq.cmd file in tandem with just the bq command

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
Run 'bq.py help' to get 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM