ModuleNotFoundError:没有名为“google.cloud”的模块

2024-06-26 11:12:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用Google的“云文本到语音”api,我有一个常见的问题,就是找不到模块。我尝试过大多数人都有的解决方案,唯一的问题是我使用windows,而且大多数解决方案都是针对mac或Linux的(尽管这不应该是个大问题)。

我在命令行上运行了“pip list”,返回如下:

google                    2.0.1
google-api-core           1.7.0
google-auth               1.6.3
google-cloud              0.34.0
google-cloud-texttospeech 0.4.0
googleapis-common-protos  1.5.8

如果这有帮助的话,这就是我在import语句上运行的代码(这也取自google的教程)

>> from google.cloud import texttospeech

from google.cloud import texttospeech
ModuleNotFoundError: No module named 'google.cloud'

有什么解决办法吗?


Tags: 模块命令行from文本importapicloudlinux
1条回答
网友
1楼 · 发布于 2024-06-26 11:12:08

ModuleNotFoundError: No module named 'google.cloud'

要解决这个问题:

  1. 删除谷歌云:pip uninstall google-cloud
  2. 使用更新google cloud texttospeech重新安装:pip install --upgrade google-cloud-textdtospeech

库google cloud已被弃用。不要安装或使用此库。

开始文字到语音转换的示例代码:

from google.cloud import texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)

# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
    audio_encoding=texttospeech.enums.AudioEncoding.MP3)

# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)

# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
    # Write the response to the output file.
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

相关问题 更多 >