Python 无法从环境变量 (Windows) 中获取 GOOGLE_APPLICATION_CREDENTIALS

Python unable to Fetch GOOGLE_APPLICATION_CREDENTIALS from Environment Variable (Windows)

我想用 Python 探索 Google Big-Query,根据这个 tutorial 我已经设置了一个 Google 云帐户(免费层) ,并生成了密钥。 JSON 文件存储在 D:\keys\quixotic-folio-318907-64bfdccfb050.json.

Windows-10中的ENVIRONMENT VARIABLES也添加到GOOGLE_APPLICATION_CREDENTIALS下的System Variables下:

但是,每当我尝试初始化客户端时,它都会抛出错误 - File Not Found:

> from google.cloud import storage
> storage.Client(project = "quixotic-folio-318907")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda3\lib\site-packages\google\cloud\storage\client.py", line 123, in __init__
    super(Client, self).__init__(
  File "D:\Anaconda3\lib\site-packages\google\cloud\client.py", line 319, in __init__
    Client.__init__(
  File "D:\Anaconda3\lib\site-packages\google\cloud\client.py", line 178, in __init__
    credentials, _ = google.auth.default(scopes=scopes)
  File "D:\Anaconda3\lib\site-packages\google\auth\_default.py", line 454, in default
    credentials, project_id = checker()
  File "D:\Anaconda3\lib\site-packages\google\auth\_default.py", line 221, in _get_explicit_environ_credentials
    credentials, project_id = load_credentials_from_file(
  File "D:\Anaconda3\lib\site-packages\google\auth\_default.py", line 107, in load_credentials_from_file
    raise exceptions.DefaultCredentialsError(
google.auth.exceptions.DefaultCredentialsError: File D:\keys\quixotic-folio-318907-64bfdccfb050.json; was not found.

我试过 os 方法,按照建议 ,运行 非常完美:

> import os
> from google.cloud import storage
> os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "D:\keys\quixotic-folio-318907-64bfdccfb050.json"
> storage.Client(project = "quixotic-folio-318907")
<google.cloud.storage.client.Client object at 0x000002448A4E8AF0>

我有以下问题:

  1. 这是预期的行为吗?为什么?
  2. 如何确保我不必专门设置 os.environ['GOOGLE_APPLICATION_CREDENTIALS'],因为它已在系统变量下定义?

删除

;

在环境变量中路径的末尾。

编辑:用户 AKS 比我快。 @AKS:如果你在答案中写下你的评论,它可以被标记为已解决。