我正在尝试使用来自 Google Natural Language API 的示例代码来获取情绪分数。但是,每次运行代码时,我都会收到“ImportError:无法导入名称语言”。第一行的错误。
我已经 pip 安装了库,尝试卸载并重新安装,在控制台上制作了凭据(API 显示已启用)并查看了本教程并完成了答案中的这些步骤:Google 情绪分析 - ImportError:无法导入名称语言。它没有帮助。还有什么我可以尝试的吗?
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