ImportError: cannot import name 'texttospeech' from 'google.cloud' (unknown location)

Viewed 3435

Trying to make an extention for my discord bot that converts the input of the user's text to an audio file so that my bot can play that audio. I just can't figure out why I can't import the google module.

from google.cloud import texttospeech


def synthesize_text(text):

    """Synthesizes speech from the input string of text."""

    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().

    voice = texttospeech.VoiceSelectionParams(

        language_code="en-US",
        name="en-US-Standard-C",
        ssml_gender=texttospeech.SsmlVoiceGender.FEMALE,
    )

    audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)

    response = client.synthesize_speech(request={"input": input_text, "voice": voice, "audio_config": audio_config})

    # The response's audio_content is binary.

    with open("output.mp3", "wb") as out:


        out.write(response.audio_content)

        print('Audio content written to file "output.mp3"')
2 Answers

As stated in the comment above, you need to first install the library on your machine before calling the function within your app.

pip install google-cloud-texttospeech

Read through official documentation on usage if required.

Based on your code I believe that you have the following item checked off as well.

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Cloud Text-to-Speech API.
  4. Setup Authentication.

This worked for me:

pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

pip install google-cloud-texttospeech
Related