Ubuntu虚拟环境中Python脚本无法导入google-cloud speech模块
Ubuntu虚拟环境中调用Google Cloud语音转写的导入错误问题
在Ubuntu本地虚拟开发环境中尝试通过Python脚本调用Google Cloud语音转写功能,但运行Flask应用时一直出现导入错误,查阅相关方案后仍无法解决。
虚拟环境pip包列表
执行pip list仅显示以下内容:
(env) localhost: pip list pip list Package Version ---------- ------- pip 22.0.2 setuptools 59.6.0
运行Flask的错误信息
执行flask run --debug时出现如下错误:
(env) localhost: flask run --debug flask run --debug * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. Usage: flask run [OPTIONS] Try 'flask run --help' for help. Error: While importing 'app', an ImportError was raised: Traceback (most recent call last): File "/var/www/openai-quickstart-python/venv/lib/python3.10/site-packages/flask/cli.py", line 218, in locate_app __import__(module_name) File "/var/www/openai-quickstart-python/app.py", line 47, in <module> transcribe_file('audio-dara.mp3') File "/var/www/openai-quickstart-python/app.py", line 25, in transcribe_file from google.cloud import speech ImportError: cannot import name 'speech' from 'google.cloud' (unknown location)
app.py代码内容
m enum import Enum import time, sys # import google.cloud.texttospeech as tts import simpleaudio as sa import logging import google.cloud # General libs app = Flask(__name__) openai.api_key = os.getenv("OPENAI_API_KEY") question1 = "Tell me about the Cork Gay Project?" question2 = "Why was the Cork Gay Project created?" def transcribe_file(speech_file): # Imports the Google Cloud client library from google.cloud import speech # Instantiates a client client = speech.SpeechClient() # The name of the audio file to transcribe gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw" audio = speech.RecognitionAudio(uri=gcs_uri) config = speech.RecognitionConfig( encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=16000, language_code="en-US", ) # Detects speech in the audio file response = client.recognize(config=config, audio=audio) for result in response.results: print("Transcript: {}".format(result.alternatives[0].transcript)) transcribe_file('audio-dara.mp3')
安装包的异常情况
尝试安装google-cloud包,但虚拟环境的pip list未显示该包;使用sudo安装时提示该包已存在于系统Python路径中:
(env) localhost: sudo pip install google-cloud sudo pip install google-cloud Requirement already satisfied: google-cloud in /usr/local/lib/python3.10/dist-packages (0.34.0) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
解决步骤
安装正确的Google Cloud语音转写库
Google Cloud客户端库采用分模块设计,google-cloud是过时的元包,语音转写需要安装专门的google-cloud-speech包。确保虚拟环境处于激活状态(命令行显示(env)),执行:pip install google-cloud-speech flask python-dotenv openai simpleaudio避免用sudo安装虚拟环境依赖
sudo会将包安装到系统全局Python环境,而非当前虚拟环境。始终在激活虚拟环境后直接用pip install命令安装依赖。修正代码中的缺失导入和语法错误
- 第一行错误的
m enum import Enum改为from enum import Enum - 添加缺失的导入语句:
from flask import Flask import openai import os
- 第一行错误的
内容的提问来源于stack exchange,提问作者Myuser03
相关产品推荐
相关产品推荐

