NameError: name 'creds' is not defined

NameError: name 'creds' is not defined

我收到一个错误,NameError: name 'creds' is not defined 。我想使用 Google Cloud Vision API.I 在 Google Cloud 中设置各种东西,我下载 google-cloud-sdk-180.0.0-darwin-x86_64.tar.gz ,我 运行 命令 ./google-cloud-sdk/bin/gcloud init,successful.I 写 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。 .我真的不明白为什么这个错误 happens.I installed config.json & index.js & package.json in the same directory as test.py but same error happens.I 运行 命令 gcloud components update && gcloud components install beta 但 zsh:找不到命令:gcloud 错误 happens.How 我应该解决这个问题吗?我的代码有什么问题?

要修复此错误,您需要定义变量 这是我在 google 云上找到的变量 creds 在客户之前添加:

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

在进口线上

from google.auth import credentials

import mock