【发布时间】:2021-03-07 06:48:46
【问题描述】:
我正在尝试使用 Python 调用 Google Cloud AI 平台培训 API。我的服务帐户密钥 JSON 文件的路径是“/Users/my_mac_username/service_account_key.json”,我在 bash_profile 文件中添加了导出语句,使其看起来像:
# Setting PATH for Python 3.8
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
export GOOGLE_APPLICATION_CREDENTIALS="/Users/my_mac_username/service_account_key.json"
但是,我仍然收到错误:
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials.
此外,我还安装了 google-cloud-sdk 文件夹。我想知道我应该在哪个文件中设置环境变量。感谢您的帮助!
【问题讨论】:
-
如果你从命令行运行
echo $GOOGLE_APPLICATION_CREDENTIALS,你会得到正确的数据吗?在 bash_profile 中设置值后,您是否打开了新控制台? -
不,我得到一个空字符串
-
运行
source ~/.bash_profile以加载您的个人资料或打开一个新的终端窗口。您也可以直接从该控制台运行export GOOGLE_APPLICATION_CREDENTIALS="/Users/my_mac_username/service_account_key.json" -
谢谢它解决了这个问题!有没有办法永久设置环境变量?
-
假设您正在运行 bash 终端,您在 ~/.bash_profile 中的设置将执行此操作。您可以通过在终端中运行
echo $SHELL来查看
标签: environment-variables google-authentication google-api-python-client