gpt4 book ai didi

python - 名称错误 : name 'creds' is not defined

转载 作者:太空宇宙 更新时间:2023-11-03 14:07:45 25 4
gpt4 key购买 nike

我收到错误,NameError:名称“creds”未定义。我想使用 Google Cloud Vision API。我在 Google Cloud 中设置了各种内容,并下载了 google-cloud-sdk-180.0.0-darwin-x86_64 .tar.gz ,我运行命令 ./google-cloud-sdk/bin/gcloud init,成功了。我写了 test.py

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient(credentials=creds,)

# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'cat.jpg')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()

当我运行这段代码时,

Traceback (most recent call last):
client = vision.ImageAnnotatorClient(credentials=creds,)
NameError: name 'creds' is not defined

发生错误。我通过查看https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/docs/vision/index.rst#id8来编写代码,所以我重写了

client = vision.ImageAnnotatorClient()

发生错误 google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式创建凭据并重新运行应用程序。了解更多信息,请参阅 https://developers.google.com/accounts/docs/application-default-credentials 。 。我真的无法理解为什么会发生这个错误。我在与 test.py 相同的目录中安装了 config.json & index.js & package.json 但发生了同样的错误。我运行命令 gcloud Components update && gcloud Components install beta 但是 zsh: 找不到命令: gcloud 发生错误。我应该如何解决这个问题?我的代码出了什么问题?

最佳答案

要修复此错误,您需要定义变量这是我在谷歌云上找到的变量信用信息在客户端之前添加此内容:

creds = mock.Mock(spec=credentials.Credentials)

以及导入行

from google.auth import credentials

import mock

关于python - 名称错误 : name 'creds' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com