Python 名称错误:名称';信誉';没有定义
我遇到一个错误,NameError:name'creds'没有定义。我想使用Google Cloud Vision API。我在Google Cloud中设置了各种东西,下载了Google-Cloud-sdk-180.0.0-darwin-x86_64.tar.gz,运行命令Python 名称错误:名称';信誉';没有定义,python,google-cloud-platform,google-vision,Python,Google Cloud Platform,Google Vision,我遇到一个错误,NameError:name'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
/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
错误发生了。
我通过看写代码,所以我重写了
client = vision.ImageAnnotatorClient()
错误发生google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置GOOGLE\u应用程序\u凭据或
显式创建凭据并重新运行应用程序。更多
信息,请参阅
. . 我真的无法理解为什么会发生此错误。我将config.json&index.js&package.json安装在与test.py相同的目录中,但发生了相同的错误。我运行命令
gcloud components update&&gcloud components install beta
,但zsh:command not found:gcloud error会发生。我应该如何修复此问题?我的代码中有什么错误?要修复此错误需要定义变量
以下是我在谷歌云上发现的关于可变信誉的信息
将此添加到客户端之前:
creds = mock.Mock(spec=credentials.Credentials)
在进口线上
from google.auth import credentials
import mock
错误就在这里,你没有定义
creds
@Phydeaux哦,真的吗?那么我应该定义什么creds?我应该写吗?非常感谢,我写了你的代码,所以google.api_core.exceptions.PermissionDenied:403请求缺少有效的api密钥。发生错误。我已获得API密钥和搜索错误的结果,我应该定义'key=myAPIKey',但我不能理解我应该如何使用key变量。我应该怎么做?这是一个key的例子:所以试着从一个文件中读取,或者通过将api key放入如下字符串来定义key:key='a22b33bb3b3'你读过谷歌云文档了吗?