ModuleNotFoundError:没有名为“google”Python和google TTS的模块

2024-06-16 13:01:34 发布

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

我有一个使用Python和Google TTS生成wav文件的批处理文件。此shell脚本在Windows 10上运行良好。我需要在LinuxWeb服务器上运行脚本(作为shell脚本),但是我得到了错误 ModuleNotFoundError: No module named 'google'

在Python shell中运行help('modules')可以为我提供当前安装的以下google模块:

google_auth_httplib2
googleapiclient
googlesearch

运行pip install --upgrade google会让我:

Collecting google
  Using cached https://files.pythonhosted.org/packages/81/51/36af1d18648574d13d8f43e863e95a97fe6f43d54a13fbcf272c638c10e9/google-2.0.3-py2.py3-none-any.whl
Collecting beautifulsoup4 (from google)
  Using cached https://files.pythonhosted.org/packages/cb/a1/c698cf319e9cfed6b17376281bd0efc6bfc8465698f54170ef60a485ab5d/beautifulsoup4-4.8.2-py3-none-any.whl
Collecting soupsieve>=1.2 (from beautifulsoup4->google)
  Using cached https://files.pythonhosted.org/packages/81/94/03c0f04471fc245d08d0a99f7946ac228ca98da4fa75796c507f61e688c2/soupsieve-1.9.5-py2.py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4, google
Successfully installed beautifulsoup4-4.8.2 google-2.0.3 soupsieve-1.9.5

但是我仍然只有上面提到的三个模块。当我运行脚本sudo -u www-data sh testfile.sh时,我得到以下错误:

Traceback (most recent call last):
  File "TTSA.py", line 37, in <module>
    f'<speak>{speech}</speak>'
  File "TTSA.py", line 9, in synthesize_ssml
    from google.cloud import texttospeech
ModuleNotFoundError: No module named 'google'

以下是Python脚本:

import sys

filename = str(sys.argv[1])
speech = str(sys.argv[2])

def synthesize_ssml(ssml):
    import os
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'creds.json'
    from google.cloud import texttospeech
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(ssml=ssml)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-GB',
        name='en-GB-Wavenet-A'
        #ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE
        )

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16,
        sample_rate_hertz=8000,
        speaking_rate=1.0
        #pitch=2
        )

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    with open(f'{filename}.wav', 'wb') as out:
        out.write(response.audio_content)
        print(f'Audio content written to file {filename}.wav')

synthesize_ssml(
    f'<speak>{speech}</speak>'
    )

正在运行的版本:

$ pip -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.7)
$ pip2 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.7)
$ pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

python3.6谷歌模块:

google_auth_httplib2
google_oauth2_tool
googleapiclient
googlesearch

python3.7谷歌模块:

google_auth_httplib2
googleapiclient
googlesearch

Tags: 模块pipfromimport脚本packagesgoogleaudio