ImportError:无法在Google Cloud Language API中导入名称语言

时间:2018-04-28 03:18:49

标签: python google-api google-cloud-platform google-cloud-functions google-natural-language

我正在尝试使用Google Natural Language API中的示例代码来获取情绪分数。但是,每次运行代码时,都会出现“ImportError:无法导入名称语言”。第一行出错。

我已经安装了pip库,尝试卸载并重新安装,在控制台上创建了凭据(显示了API已启用)并查看了本教程,并在答案中完成了这些步骤:Google sentiment analysis - ImportError: cannot import name language。它没有帮助。还有什么我可以尝试的吗?

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

client = language.LanguageServiceClient()

text = u'Hello, world!'
document = types.Document(
    content=text,
    type=enums.Document.Type.PLAIN_TEXT)

sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

我也用适当的路径将它粘贴到我的终端中。

export GOOGLE_APPLICATION_CREDENTIALS="/....(my path)/service_key.json"

堆栈追踪:

Traceback (most recent call last):
  File "lang.py", line 3, in <module>
    from google.cloud import language
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/language.py", line 17, in <module>
    from google.cloud.language_v1 import LanguageServiceClient
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/language_v1/__init__.py", line 17, in <module>
    from google.cloud.language_v1 import types
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/language_v1/types.py", line 18, in <module>
    from google.api_core.protobuf_helpers import get_messages
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/api_core/__init__.py", line 20, in <module>
    from pkg_resources import get_distribution
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3161, in <module>
    @_call_aside
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3145, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3189, in _initialize_master_working_set
    for dist in working_set
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3189, in <genexpr>
    for dist in working_set
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2715, in activate
    declare_namespace(pkg)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2274, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2209, in _handle_ns
    loader.load_module(packageName)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 246, in load_module
    mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  File "/.../lang.py", line 3, in <module>
    from google.cloud import language
ImportError: cannot import name language

4 个答案:

答案 0 :(得分:2)

解释:

如果查看堆栈跟踪,google.cloud.language的导入实际上是有效的,它不是循环的。堆栈跟踪中的第二项和第三项是langauge.py成功请求下面的项目,最终委托给google.api_core(这是我们所有这些库后面的运行时)。

跟踪中的第五行是有趣的:它对应于google/api_core/__init__.py的第20行,它是from pkg_resources import get_distribution。之后的所有事情都是为了使这种导入工作;因为它没有,ImportError冒泡,而之前的导入级联失败。

可能的解决方案:

确保您的pipsetuptools是最新的。命名空间打包非常棘手,所以你必须拥有一个非常新的版本。只需发出pip install --upgrade setuptools pip

Gordian解决方案:

您是否考虑过Python 3? : - )

<强>疑难解答:

如果这不起作用(并且Python 3不是一个选项),接下来我们需要知道的是最终的失败是什么。跟踪中的倒数第二个呼叫是对imp.load_module(fullname, self.file, self.filename, self.etc)的呼叫。我们需要知道这些值是什么来进一步排除故障。要获取它们,请在导入失败之前立即添加import pdb ; pdb.set_trace()。这将使您在此时投入调试器。使用n(下一个)和s(步入函数)来遍历代码(您可以通过在REPL中键入变量值来获取变量值)。如果您可以打印特定要导入的内容的值,我们可以为您提供进一步的帮助。

答案 1 :(得分:0)

这似乎是该问题的重复:

Google sentiment analysis - ImportError: cannot import name language

对我来说,还不足以升级google-api-python-client和google-cloud

相反,解决我问题的是:

!pip install google-cloud-language

此外,当升级google api库时,awsebcli库(来自AWS)显示不兼容错误。

答案 2 :(得分:0)

尝试升级点数:

python -m pip install --upgrade pip

然后升级Google软件包:

pip install --upgrade google-api-python-client
pip install --upgrade google-cloud

答案 3 :(得分:0)

如果使用Google Cloud Functions,请确保在requirements.txt标签中指定了google-cloud-language:enter image description here