wangzhensuo 2020-06-15 08:41 采纳率: 0%
浏览 260

python程序,如果设置console=False,用pyinstaller打包成exe,运行出错“Failed to execute script”

1.首先,这是个将文件夹中的MP3音频合并成一个mp3文件的python程序。

def output_mp3(directory,silence_times):
    import os
    from pydub import AudioSegment
    import time

    silence_times = silence_times*1000
    # get files' name
    file_list = []
    for file in os.listdir(directory):
        file_list.append(os.path.join(directory, file))
    print(file_list)
    # get the audio files
    sounds = []
    for file in file_list:
        sounds.append(AudioSegment.from_mp3(file))
        print('正在连接文件',file)

    # print('完成文件连接!')
    # generate a silence ring file
    silence_ring = AudioSegment.silent(int(silence_times))
    # merge the rings mixed with silence file
    word_lists = AudioSegment.empty()
    for sound in sounds:
        # print(time.strftime("---正在合成mp3文件---%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) )
        for repeat_t in range(0,1):
            word_lists += sound
            word_lists += silence_ring

    # print('再稍等几秒钟,正在保存文件')
    now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) 
    word_lists.export('合成单词{}.mp3'.format(now), format="mp3")
    print('---保存完成,请到软件目录查看合成的音频文件----')
    # self.MP3_show_message_signal.emit('保存完成,请到软件目录查看合成的音频文件.')
output_mp3('.\\mp3\\',1)

pyinstaller用的test.spec文件内容如下:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['test.py'],
             pathex=['G:\\vscode_python\\WF\\test'],
             binaries=[],
             datas=['G:\\vscode_python\\WF\\test\\dist\\mp3'],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False )

如果我设置console=False,则打包后的exe运行出错。“Failed to execute script”。
如果我设置console=True,则打包后的exe会出现命令行,但运行是没有错误。
查了库pydub,issue里面提到了,但是说这个问题已经修正了。
https://github.com/pyinstaller/pyinstaller/issues/2015
网上找了比较类似的问题,不是这个pydub库打包,是其他的库。
里面说是因为标准输入是空的,加上PIPE就好了。
https://stackoverflow.com/questions/33867183/python-program-with-seleniumwebdriver-dont-work-as-single-and-noconsole-exe-f
修改方式如下,

#modified the code from:

self.process = subprocess.Popen(cmd,
                stdout=PIPE, stderr=PIPE)
#To:

self.process = subprocess.Popen(cmd, stdin=PIPE,
                stdout=PIPE, stderr=PIPE)

但是我查看pydub里面是下面这样。是有sdtin的。所以不知道怎么改?
想请熟悉subprocess的大侠帮忙看看,谢谢了。

        with open(os.devnull, 'rb') as devnull:
            p = subprocess.Popen(conversion_command, stdin=devnull, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p_out, p_err = p.communicate()
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-08-26 16:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥15 微信原生小程序tabBar编译报错
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题