Different ways of setting environment variables in Jupyter notebook

Viewed 2799

For some context, I am using Jupyter notebook on a Windows 10 machine and I want to authenticate myself into GCP by setting the environment variable "GOOGLE_APPLICATION_CREDENTIALS".

I was wondering, what is the difference between the two ways of setting environment variables? The first approach works when I try to access Big Query but the second doesn't.

First approach,

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path-to-json-file-windows-style-dir'

Second approach using magic command,

%env GOOGLE_APPLICATION_CREDENTIALS="path-to-json-file-windows-style-dir"

When I attempt to use the env variable after setting it using the second method I get the error

DefaultCredentialsError: File path-to-json-file-windows-style-dir was not found.

which is weird because the error is printing out the correct value of the environment variable and the path is correct.

This is the code I used for access data from Big Query which is taken directly from Google's documentation:

from google.cloud import bigquery

client = bigquery.Client()

# Perform a query.
QUERY = (
    'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
    'WHERE state = "TX" '
    'LIMIT 100')
query_job = client.query(QUERY)  # API request
rows = query_job.result()  # Waits for query to finish

for row in rows:
    print(row.name)
1 Answers

Turns out the two methods of setting the variables yields an identical result. The problem was resolved because I added quotes to the variable in the first method which became part of the variable.

Related