google cloud speech ImportError: cannot import name 'enums'
Asked Answered
B

4

9

I'm using google-cloud-speech api for my project . I'm using pipenv for virtual environment i installed google-cloud-speech api with

pipenv install google-cloud-speech

and

pipenv update google-cloud-speech

i followed this docs https://cloud.google.com/speech-to-text/docs/reference/libraries

This is my code:

google.py:

# !/usr/bin/env python
# coding: utf-8
import argparse
import io
import sys
import codecs
import datetime
import locale
import os

from google.cloud import speech_v1 as speech
from google.cloud.speech import enums
from google.cloud.speech import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join("alt_speech_dev_01-fa5fec6806d9.json")
def get_model_by_language_id(language_id):
    model = ''
    if language_id == 1:
        model = 'ja-JP'
    elif language_id == 2:
        model = 'en-US'
    elif language_id == 3:
        model = "zh-CN"
    else:
        raise ('Not Match Lang')
    return model

def transcribe_gcs_without_speech_contexts(audio_file_path, model):
    client = speech.SpeechClient()
    with io.open(audio_file_path, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = {
        "encoding": enums.RecognitionConfig.AudioEncoding.FLAC,
        "sample_rate_hertz": 16000,
        "languageCode": model
        }



    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    operationResult = operation.result()

    ret=''
    for result in operationResult.results:
      for alternative in result.alternatives:
          ret = alternative.transcript

    return ret

def transcribe_gcs(audio_file_path, model, keywords=None):
    client = speech.SpeechClient()
    with io.open(audio_file_path, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = {
        "encoding": enums.RecognitionConfig.AudioEncoding.FLAC,
        "sample_rate_hertz": 16000,
        "languageCode": model,
        "speech_contexts":[{"phrases":keywords}]
        }



    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    operationResult = operation.result()

    ret=''
    for result in operationResult.results:
      for alternative in result.alternatives:
          ret = alternative.transcript

    return ret

transcribe_gcs_without_speech_contexts('alt_en.wav', get_model_by_language_id(2)) 

When i try to run the python file with

python google.py

it return error ImportError: cannot import name 'SpeechClient' with the following traceback:

Traceback (most recent call last):
  File "google.py", line 11, in <module>
    from google.cloud import speech_v1 as speech
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 18, in <module>
    import pkg_resources
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in _initialize_master_working_set
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in <genexpr>
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2776, in activate
    declare_namespace(pkg)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2275, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2208, in _handle_ns
    loader.load_module(packageName)
  File "/home/hoanglinh/Documents/practice_speech/google.py", line 12, in <module>
    from google.cloud.speech import enums
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech.py", line 19, in <module>
    from google.cloud.speech_v1 import SpeechClient
ImportError: cannot import name 'SpeechClient'

Am i doing something wrong ? when i search the error online there only 1 question with no answer to it

UPDATE: i changed from

google.cloud import speech_v1 as speech

to this

from google.cloud import speech

now i got another return error with traceback like so

Traceback (most recent call last):
  File "google.py", line 11, in <module>
    from google.cloud import speech
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech.py", line 19, in <module>
    from google.cloud.speech_v1 import SpeechClient
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 18, in <module>
    import pkg_resources
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in _initialize_master_working_set
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in <genexpr>
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2776, in activate
    declare_namespace(pkg)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2275, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2208, in _handle_ns
    loader.load_module(packageName)
  File "/home/hoanglinh/Documents/practice_speech/google.py", line 12, in <module>
    from google.cloud.speech import enums
ImportError: cannot import name 'enums'

Have anyone tried this library before ? because it seem there so much errors just with following the docs of its

Bodycheck answered 28/6, 2019 at 2:34 Comment(1)
how did you solve? I'm getting similar error: ImportError: cannot import name 'enums' from 'google.cloud.speech_v1' when I deleted v1 then getting: ImportError: cannot import name 'enums' from 'google.cloud.speech' Is there any idea to solve?Uvula
T
7

The following error message is seen

    from google.cloud.speech import enums
ImportError: cannot import name 'enums' 

if an 'new' installation of the google speech api was performed. Please see this page.

Along the same lines usage of nanos attributes would result in the following message if you have updated the api

AttributeError: 'datetime.timedelta' object has no attribute 'nanos'

Please see this page. Use 'microseconds' instead of 'nanos'.

Theodicy answered 9/10, 2020 at 11:38 Comment(0)
G
1
  • First solution try to check your python3.6/site-packages/google/cloud if there is speech_v1. if there is none, you need to install it first
  • Second solution try to check your python3.6/site-packages/google/cloud if there is an existing speech file, if it exists then the cause of the import is shadowing. since your alias is 'speech' Hope this helps

try this line of codes if your using speech_v1:

from google.cloud import speech_v1 as speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types

speech:

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
Grotto answered 28/6, 2019 at 2:50 Comment(11)
there is a speech.py file in the python3.6/site-packages/google/cloud pathBodycheck
change your alias of speech_v1 and add from google.cloud import speechGrotto
you are getting this error because speech_v1 don't have a module of SpeechClient(). SpeechClient() is in speech.pyGrotto
now it return from google.cloud.speech import enums ImportError: cannot import name 'enums' which sound like a step back :/Bodycheck
That issue is connected to the new update of google cloud speech. check the update aboveGrotto
now i got from google.cloud.speech_v1 import enums ImportError: cannot import name 'enums', i think import enums and types should use speech instead of speech_v1Bodycheck
if you're using google-cloud-speech 1.0.0 they changed it to speech_v1. If not use speech instead of speech_v1.Grotto
Your imports are somewhat contradicting. You've import speech_v1 but you're using speech.py modules.Grotto
i just check package it's indeed 1.0.0 "google-cloud-speech==1.0.0" i also changed the import like you said but it still has the import error "from google.cloud.speech import enums ImportError: cannot import name 'enums'" :(Bodycheck
Let us continue this discussion in chat.Grotto
not worked in my case; I'm getting the error: cannot import name 'enums' from 'google.cloud.speech' Do you have a solution or idea?Uvula
T
1

If you can check this link. Google has moved the AudioEncodings under google.cloud.speech_v1.types you can use it by importing types and then running the code below:

from google.cloud.speech_v1 import types

types.RecognitionConfig.AudioEncoding.LINEAR16
Teacup answered 5/3, 2021 at 13:35 Comment(0)
B
0

From Google Cloud documentation :

Enums and Types WARNING: Breaking change

The submodules enums and types have been removed.

Before:

from google.cloud import videointelligence

features = [videointelligence.enums.Feature.SPEECH_TRANSCRIPTION]
video_context = videointelligence.types.VideoContext()
After:

from google.cloud import videointelligence

features = [videointelligence.Feature.SPEECH_TRANSCRIPTION]
video_context = videointelligence.VideoContext()
Bahadur answered 8/6, 2021 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.