ImportError: cannot import name language in Google Cloud Language API

11,400

Solution 1

This seems to be a duplicate of this question:

Google sentiment analysis - ImportError: cannot import name language

For me, wasn't enough to upgrade google-api-python-client and 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).

Solution 2

The explanation:

If you look at the stack trace, the import of google.cloud.language is actually working and it is not circular. 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).

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

Probable solution:

Make sure your pip and setuptools are up to date. Namespace packing is notoriously tricky so you have to have a pretty recent version. Just issue pip install --upgrade setuptools pip.

Gordian solution:

Have you considered 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. The penultimate call in the track is a call to 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() immediately before the import in your code that is failing. 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). If you can print the values of what is trying to be imported specifically, we can assist you further.

Solution 3

Try to upgrade pip:

python -m pip install --upgrade pip

Then upgrade the Google packages:

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

Solution 4

If using Google Cloud Functions, make sure google-cloud-language is specified in the requirements.txt tab: enter image description here

Share:
11,400
curious4cs
Author by

curious4cs

Updated on June 14, 2022

Comments

  • curious4cs
    curious4cs almost 2 years

    I am trying to use this sample code from the Google Natural Language API to get a sentiment score back. However, each time I run the code, I get an "ImportError: cannot import name language." 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. 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
    
  • Selaron
    Selaron about 5 years
    Please explain what this is doing and how this helps solving the problem.
  • Muhammad Haseeb Khan
    Muhammad Haseeb Khan over 4 years
    It also worked in my case. I just did 'pip install google-cloud-language'. Apparently only installing google-cloud wasn't enough. google-cloud-language needed to installed additionally.