Pylint:Module/Instance 没有 google.cloud.vision API 的成员

Pylint: Module/Instance of has no member for google.cloud.vision API

当我 运行 此代码(稍后将用于使用 Python 中的 Google Vision API 检测和提取文本)时,我收到以下错误:

Module 'google.cloud.vision_v1.types' has no 'Image' member pylint(no-member)

Instance of 'ImageAnnotatorClient' has no 'text_detection' member pylint(no-member)

from google.cloud import vision
from google.cloud.vision import types
import os, io

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'C:\Users\paul\VisionAPI\key.json'
client = vision.ImageAnnotatorClient()

FILE_NAME = 'im3.jpg'
FOLDER_PATH = r'C:\Users\paul\VisionAPI\images'


with io.open(os.path.join(FOLDER_PATH , FILE_NAME), 'rb') as image_file:
    content = image_file.read()

image = vision.types.Image(content=content)
response = client.text_detection(image=image)

“___ 的 Module/Instance 没有成员”是什么意思?

我能够重现 pylint 错误,尽管脚本在 运行 时成功执行(对我的环境进行了少量修改以更改正在处理的文件名)。

因此,我假设 "run this code" 是指 "run this code through pylint"。如果不是,请用您如何以生成 pylint 错误的方式执行代码来更新问题。

This page 描述了您看到的具体错误,以及导致误报的情况。这可能正是您遇到的误报。

Google Cloud Vision 模块似乎动态创建了这些成员,而 pylint 无法检测到它们在 运行 时确实存在,因此它引发了错误。

两个选项:

  • 按照上面链接的页面中的建议,用 # pylint: disable=no-member 注释标记受影响的行。
  • 运行 pylint 带有 --ignore-modules=google.cloud.vision_v1 标志(或将等效项放入您的 .pylintrc 中)。您会注意到,即使是实际的模块名称也不同于您导入的模块名称:)

这是一个 ,其中包含有关 E1101 错误解决方法的更多详细信息。