简体   繁体   English

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

[英]ImportError: cannot import name language in Google Cloud Language API

I am trying to use this sample code from the Google Natural Language API to get a sentiment score back.我正在尝试使用 Google Natural Language API 中的示例代码来获取情绪分数。 However, each time I run the code, I get an "ImportError: cannot import name language."但是,每次运行代码时,我都会收到“ImportError:无法导入名称语言”。 error on the first line.第一行错误。

I have pip installed the library, tried uninstalling and reinstalling, made the credentials on the console (the API is shown to be enabled) and looked at this tutorial too and completed those steps in the answer: Google sentiment analysis - ImportError: cannot import name language .我已经 pip 安装了库,尝试卸载并重新安装,在控制台上创建凭据(API 显示为已启用)并查看了本教程并完成了答案中的这些步骤: Google 情绪分析 - ImportError:无法导入名称语言 It hasn't helped.它没有帮助。 Is there anything else I can try?还有什么我可以尝试的吗?

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))

I also have pasted this into my terminal with the proper path.我还使用正确的路径将其粘贴到我的终端中。

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

Stack trace:堆栈跟踪:

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

This seems to be a duplicate of this question:这似乎是这个问题的重复:

Google sentiment analysis - ImportError: cannot import name language 谷歌情绪分析 - ImportError:无法导入名称语言

For me, wasn't enough to upgrade google-api-python-client and google-cloud对我来说,升级 google-api-python-client 和 google-cloud 还不够

Instead, what solved my problem was:相反,解决我的问题的是:

!pip install google-cloud-language

Besides, when you upgrade google api libraries, an incompatibility error shows up with awsebcli library (from AWS).此外,当您升级 google api 库时,awsebcli 库(来自 AWS)会出现不兼容错误。

The explanation:说明:

If you look at the stack trace, the import of google.cloud.language is actually working and it is not circular.如果您查看堆栈跟踪, google.cloud.language的导入实际上是有效的,它不是循环的。 The second and third items in the stack trace are langauge.py successfully asking for the items underneath, ultimately delegating off to google.api_core (which is our runtime behind all of these libraries).堆栈跟踪中的第二项和第三项是langauge.py成功请求下面的项,最终委托给google.api_core (这是我们所有这些库背后的运行时)。

The fifth line in the trace is the interesting one: it corresponds to line 20 of google/api_core/__init__.py and it is from pkg_resources import get_distribution . trace 中的第五行很有趣:它对应于google/api_core/__init__.py的第 20 行, from pkg_resources import get_distribution Everything that comes after that is an attempt to make that import work;之后发生的一切都是为了使导入工作的尝试; since it does not, the ImportError bubbles up, and the previous imports cascade-fail.因为它没有, ImportError冒泡,并且之前的导入级联失败。

Probable solution:可能的解决方案:

Make sure your pip and setuptools are up to date.确保您的pipsetuptools是最新的。 Namespace packing is notoriously tricky so you have to have a pretty recent version.命名空间打包是出了名的棘手,所以你必须有一个非常新的版本。 Just issue pip install --upgrade setuptools pip .只需发出pip install --upgrade setuptools pip

Gordian solution:快速解决方案:

Have you considered Python 3?你考虑过 Python 3 吗? :-) :-)

Troubleshooting:故障排除:

If that does not work (and Python 3 is not an option), the next thing we need to know is what that final failure is.如果这不起作用(并且 Python 3 不是一个选项),接下来我们需要知道的是最终的失败是什么。 The penultimate call in the track is a call to imp.load_module(fullname, self.file, self.filename, self.etc) .轨道中的倒数第二个调用是对imp.load_module(fullname, self.file, self.filename, self.etc)的调用。 We will need to know what those values are to troubleshoot further.我们需要知道这些值是什么才能进一步排除故障。 To get them, add import pdb; pdb.set_trace()要获取它们,请添加import pdb; pdb.set_trace() import pdb; pdb.set_trace() immediately before the import in your code that is failing. import pdb; pdb.set_trace()紧接在导入失败的代码之前。 This will toss you into a debugger at that point.这会把你扔进调试器。 Use n (next) and s (step into function) to move through the code (you can get variable values and such by typing them in the REPL).使用n (下一步)和s (进入函数)在代码中移动(您可以通过在 REPL 中键入它们来获取变量值等)。 If you can print the values of what is trying to be imported specifically, we can assist you further.如果您可以打印出具体要导入的值,我们可以进一步帮助您。

Try to upgrade pip:尝试升级pip:

python -m pip install --upgrade pip

Then upgrade the Google packages:然后升级谷歌包:

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

If using Google Cloud Functions, make sure google-cloud-language is specified in the requirements.txt tab:如果使用 Google Cloud Functions,请确保在 requirements.txt 选项卡中指定了 google-cloud-language: 在此处输入图像描述

The following solved my issue:以下解决了我的问题:

pip install google-cloud-translate

Then i ran the following code smoothly:然后我顺利的跑了下面的代码:

from google.cloud import translate

reference: https://pypi.org/project/google-cloud-translate/参考: https://pypi.org/project/google-cloud-translate/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImportError:无法从“google.cloud”(未知位置)导入名称“aiplatform” - ImportError: cannot import name 'aiplatform' from 'google.cloud' (unknown location) AWS CDK - 导入错误:无法从“aws_cdk.cloud_assembly_schema”导入名称“AssetManifestOptions” - AWS CDK - ImportError: cannot import name 'AssetManifestOptions' from 'aws_cdk.cloud_assembly_schema' 导入谷歌云 ai 平台时无法从 'shapely.geos' 导入名称 'WKBWriter' - cannot import name 'WKBWriter' from 'shapely.geos' when import google cloud ai platform 如何修复 ImportError: cannot import name '_mysql' in Django 2.2.5 - How fix ImportError: cannot import name '_mysql' in Django 2.2.5 导入错误:无法从“sklearn.externals”导入名称“joblib” - ImportError: cannot import name 'joblib' from 'sklearn.externals' 邮递员请求:ImportError:无法导入名称 DEFAULT_CIPHERS - Postman request : ImportError: cannot import name DEFAULT_CIPHERS ImportError:无法为 Python 导入 Cloud Firestore 库。确保安装“google-cloud-firestore”模块 - ImportError: Failed to import the Cloud Firestore library for Python. Make sure to install the "google-cloud-firestore" module 不确定如何解决来自 Google 自然语言 api 的语言错误消息:“document_sentiment 分析不支持语言 sq。” - Unsure how to resolve language error message from Google's natural language api: "The language sq is not supported for document_sentiment analysis." 无法从“grpc._cython”导入名称“cygrpc”- Google Ads API - Cannot import name 'cygrpc' from 'grpc._cython' - Google Ads API 为 Google AutoML 设置语言 - Set a language for Google AutoML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM