ImportError: cannot import name 'types' from 'google.cloud.vision' though I have google cloud vision installed

ImportError: cannot import name 'types' from 'google.cloud.vision' though I have google cloud vision installed

我已经按照文档安装了 google-cloud-vision 库。由于某种原因无法从 google.cloud.vision 导入 types。它在我的电脑上运行良好,现在当我与我的客户共享时,他遇到了导入问题,尽管他通过 pip 安装了库。这是抛出错误的行:

from google.cloud import vision
from google.cloud.vision import types # this line throws error

知道如何解决这个问题吗?

这可能是因为存在某些版本不匹配(或者不太可能存在其他具有相同名称的库)。让您的客户使用虚拟环境。这应该可以解决问题。

P.S。你必须向他提供一个 requirements.txt 文件(从 pip3 freeze 获得),这样他就可以在他的虚拟环境中执行 pip3 install -r requirements.txt 以获得与你的完全相同的包。

类型模块已从 google.cloud.vision 2.0.0 中删除。您可以从视觉访问所有类型。

https://googleapis.dev/python/vision/latest/UPGRADING.html#enums-and-types

之前:

from google.cloud import vision_v1

likelihood = vision_v1.enums.Likelihood.UNKNOWN
request = vision_v1.types.GetProductSetRequest(name="name")

之后:

from google.cloud import vision_v1

likelihood = vision_v1.Likelihood.UNKNOWN
request = vision_v1.GetProductSetRequest(name="name")

使用from google.cloud.vision_v1 import types代替from google.cloud.vision import types。I have get this by exploring the init.py file and it works.