如何在另一个python文件中运行python文件?
我很难在另一个python文件中作为模块运行python文件。我试图在另一个python文件中运行的程序本身运行良好。但是,当我将其作为模块导入时,它不会执行任何操作,甚至不会给我错误代码。 这是第一个代码。文件名为speech2text.py如何在另一个python文件中运行python文件?,python,Python,我很难在另一个python文件中作为模块运行python文件。我试图在另一个python文件中运行的程序本身运行良好。但是,当我将其作为模块导入时,它不会执行任何操作,甚至不会给我错误代码。 这是第一个代码。文件名为speech2text.py def mainprogram(): import os import sys os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"C:\Users\taiki\PycharmPro
def mainprogram():
import os
import sys
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"C:\Users\taiki\PycharmProjects\startup\stelarvision-280712-c709366612cc.json"
filepath = "./"
output_filepath = "./"
bucketname = "stelarvision2020"
sys.path.append("/users/taiki/appdata/local/packages/pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0/localcache/local-packages/python38/site-packages")
sys.path.append("/Users/taiki/AppData/Local/Programs/Python/Python38-32/Lib/site-packages")
from pydub import AudioSegment
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
import wave
from google.cloud import storage
def stereo_to_mono(audio_file_name):
sound = AudioSegment.from_wav(audio_file_name)
sound = sound.set_channels(1)
sound.export(audio_file_name, format="wav")
def frame_rate_channel(audio_file_name):
with wave.open(audio_file_name, "rb") as wave_file:
frame_rate = wave_file.getframerate()
channels = wave_file.getnchannels()
return frame_rate, channels
def upload_blob(bucket_name, source_file_name, destination_blob_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
def delete_blob(bucket_name, blob_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
blob.delete()
def google_transcribe(audio_file_name):
file_name = filepath + audio_file_name
frame_rate, channels = frame_rate_channel(file_name)
if channels > 1:
stereo_to_mono(file_name)
bucket_name = bucketname
source_file_name = filepath + audio_file_name
destination_blob_name = audio_file_name
upload_blob(bucket_name, source_file_name, destination_blob_name)
gcs_uri = 'gs://' + bucketname + '/' + audio_file_name
transcript = ''
client = speech.SpeechClient()
audio = types.RecognitionAudio(uri=gcs_uri)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=frame_rate,
language_code='en-US',
enable_automatic_punctuation=True)
operation = client.long_running_recognize(config, audio)
response = operation.result(timeout=10000)
for result in response.results:
transcript += result.alternatives[0].transcript
delete_blob(bucket_name, destination_blob_name)
return transcript
def write_transcripts(transcript_filename, transcript):
f = open(output_filepath + transcript_filename, "w+")
f.write(transcript)
f.close()
if __name__ == "__main__":
audio_file_name = "sample_music.wav"
transcript = google_transcribe(audio_file_name)
transcript_filename = audio_file_name.split('.')[0] + '.txt'
write_transcripts(transcript_filename, transcript)
mainprogram()
这是第二个python文件
def run():
import speech2text
speech2text.mainprogram()
run()
当我执行第二个文件时,它不会做任何事情。如果uuuu name_uuu==“uuuu main_uuu”:仅在直接执行该脚本时执行,即
python speech2text.py
要使程序正常工作,请删除if语句。python文件的结构有点奇怪。最好在文件的开头导入sys和操作系统,而不是
导入类中的内容。
此外,您的缩进有问题。在我看来,你最好下载一个像pycharm这样的优秀IDE来自动检查你的语法错误 首先,您应该将导入放在函数之外,因为它更为常规。您还应该检查两个文件是否在同一文件夹中
第二,如果你把它叫到外面,这部分就不起作用了
if __name__ == "__main__":
audio_file_name = "sample_music.wav"
transcript = google_transcribe(audio_file_name)
transcript_filename = audio_file_name.split('.')[0] + '.txt'
write_transcripts(transcript_filename, transcript)
这一个用来表示当从另一个程序调用时,不会自动运行,基本上你现在正在这样做
第三,第一个文件中的最后一行mainprogram()
,不确定这里的意思。导入后,您已经有了在第二个程序speech2text.mainprogram()
中运行函数的命令
也许你只是犯了一个错误,而不是我建议的全部3个错误,所以请尝试测试 假设第二个py文件名为2,第一个py文件名为1
现在要运行二合一,请尝试以下操作
将这一行添加到一个导入到两个导入中
并运行其函数2.run()
你们两个应该是这样的
两个学习基本Python的人访问我的youtube频道如果uuuu name uuuu==“uuuuu main uuuu”:
部分只能在您直接运行speech2text.py
时执行。因此,请将代码作为函数体,然后运行speech2text.mainprogram.main()
不要使用if\uuuuuu name\uuuuu==“\uuuuu main\uuuuu”:
@oyster谢谢@pritamsamanta谢谢你!据我所知,凹痕似乎很好。另外,OP说没有错误信息。这就是说,该结构实际上没有遵循任何Python约定。但这不是问题所在。看看第一个文件的末尾,mainprogram()
在这里没有缩进。如果他或她想调用第二个文件中的mainprogram()
函数,他不必添加If\uuuuuuuu name\uuuuu==“\uuuuuu main\uuuu”:
和以下内容。事实上,这就是问题的原因problem@debug谢谢你我会处理掉的谢谢你我会试着修一下