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