0

refer to url ImportError: cannot import name 'enums'

google cloud speech ImportError: cannot import name 'enums'

I got the error when using google-cloud-speech api for my project. I'm NOT using pipenv for virtual environment. I installed google-cloud-speech api with

pip3 install google-cloud-speech

and

pip3 update google-cloud-speech

environment win10 python3.6.8

google-api-core 1.14.2

google-auth 1.6.3

google-cloud 0.34.0

google-cloud-core 1.0.3

google-cloud-speech 1.2.0

google-cloud-storage 1.18.0

google-resumable-media 0.3.3

googleapis-common-protos 1.6.0

grpc-google-cloud-speech-v1beta1 1.0.1

enum34 1.1.6 enums 0.0.2

the error contents

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

I already tried the pip command follow.

pip3 install enums
pip3 install enum34

pip3 install google-cloud-speech 
pip3 upgrade google-cloud-speech 

here the code when the code did in google cloud shell of GCP, the following code works well and I didn't get the error.

# !/usr/bin/env python
# coding: utf-8
import argparse
import io
import sys
import codecs
import datetime
import locale
from google.cloud import storage

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

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


def transcribe_gcs(gcs_uri):
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.FLAC, # wavsetting
        sample_rate_hertz=44100, # hertz must be fix raw files
        language_code='ja-JP') #for japanese language

    operation = client.long_running_recognize(config, audio)

    print('Waiting for operation to complete...')
    operationResult = operation.result()

    d = datetime.datetime.today()
    today = d.strftime("%Y%m%d-%H%M%S")
    fout = codecs.open('output{}.txt'.format(today), 'a', 'utf-8')

    for result in operationResult.results:
      for alternative in result.alternatives:
          fout.write(u'{}\n'.format(alternative.transcript))
          print('alternative.transcript===',format(alternative.transcript))
    fout.close()

    print('alternative.transcript===',format(alternative.transcript))
    print('operationResult===',operationResult)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        'path', help='GCS path for audio file to be recognized')
    args = parser.parse_args()
    transcribe_gcs(args.path)

Thanks in advance.

Brendan
  • 1,017
  • 5
  • 7
KENOSIN
  • 1
  • 1
  • 2

3 Answers3

3

By using pip install google.cloud.speech, you are getting the latest version, currently V2.

In V2, enums and types have been removed and are no longer needed.

https://github.com/googleapis/python-speech/blob/master/UPGRADING.md#enums-and-types

0

Should be: from google.cloud.speech_v1 import enums

Brendan
  • 1,017
  • 5
  • 7
  • It was just as you say that.I overlooked the code to add the version of gcs.Thanks lots Brendan. – KENOSIN Dec 12 '19 at 15:59
0

Try using,

from google.cloud.language_v1 import *

Then you can access all of followings directly

LanguageServiceAsyncClient
AnalyzeEntitiesRequest
AnalyzeEntitiesResponse
AnalyzeEntitySentimentRequest
AnalyzeEntitySentimentResponse
AnalyzeSentimentRequest
AnalyzeSentimentResponse
AnalyzeSyntaxRequest
AnalyzeSyntaxResponse
AnnotateTextRequest
AnnotateTextResponse
ClassificationCategory
ClassifyTextRequest
ClassifyTextResponse
DependencyEdge
Document
EncodingType
Entity
EntityMention
LanguageServiceClient
PartOfSpeech
Sentence
Sentiment
TextSpan
Token