加速用Pyins创建的.exe

2024-05-17 10:56:33 发布

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

我已经使用Pyinstaller将我的程序(用Python 3.6.1编写,用python3.5.3转换)从.py转换为.exe。然而,它的加载速度非常慢(与在空闲状态下运行时的1秒相比,大约需要16秒),即使在我优化了问题之后(导入大量模块,因此,我将代码更改为只导入必要的模块部分。当在空闲状态下运行时,这会大大加快速度,但是当我用它创建一个.exe时,它完全相同(并且我确实检查了我是否使用了正确的.py文件)。我觉得Pyinstaller只是将系统上安装的所有模块打包到.exe中,而不是只打包实际使用的模块的一小部分(使用--onefile)。如何确保Pyinstaller只安装模块的必要部分,或者在使用--onefile并将其打包到单个.exe中的情况下加快速度?

完整代码:

from os import path, remove
from time import sleep
from sys import exit
from getpass import getuser
from mmap import mmap, ACCESS_READ


my_file = "Text To Speech.mp3"
username = getuser()
no_choices = ["no", "nah", "nay", "course not", "don't", "dont", "not"]
yes_choices = ["yes", "yeah", "course", "ye", "yea", "yh", "do"]


def check_and_remove_file():

    active = mixer.get_init()
    if active != None:
        mixer.music.stop()
        mixer.quit()
        quit()
    if path.isfile(my_file):
        remove(my_file)


def get_pause_duration(audio_length, maximum_duration=15):

    default_pause, correction = divmod(audio_length, 12)
    return min(default_pause + bool(correction), maximum_duration)


def exiting():

    check_and_remove_file()
    print("\nGoodbye!")
    exit()


def input_for_tts(message):

    try:

        tts = gTTS(text = input(message))
        tts.save('Text To Speech.mp3')
        with open(my_file) as f:
            m = mmap(f.fileno(), 0, access=ACCESS_READ)
        audio = MP3(my_file)
        audio_length = audio.info.length
        try:
            mixer.init()
        except error:
            print("\nSorry, no audio device was detected. The code cannot complete.")
            m.close()
            exiting()   
        mixer.music.load(m)
        mixer.music.play()
        sleep(audio_length + get_pause_duration(audio_length))
        m.close()
        check_and_remove_file()

    except keyboardInterrupt:

        exiting()


from pygame import mixer, quit, error
from gtts import gTTS
from mutagen.mp3 import MP3


check_and_remove_file()


input_for_tts("Hello there " + username + ". This program is\nused to output the user's input as speech.\nPlease input something for the program to say: ")


while True:

    try:

        answer = input("\nDo you want to repeat? ").strip().lower()
        if answer in ["n", no_choices] or any(x in answer for x in no_choices):
            exiting()
        elif answer in ["y", yes_choices] or any(x in answer for x in yes_choices):
            input_for_tts("\nPlease input something for the program to say: ")
        else:
            print("\nSorry, I didn't understand that. Please try again with yes or no.")

    except KeyboardInterrupt:

        exiting()

Tags: 模块noinfromimportforinputmy
2条回答

尝试创建一个虚拟环境并从那里运行您的项目。然后从虚拟环境中运行pyinstaller,这样就只打包需要的东西。这对你最有用

其次,onedir选项比onefile快,因为它不必将所有文件从exe解压到临时文件夹中。Pyinstaller可以很容易地使用qny其他安装程序将其移动到程序文件中,并在start或其他东西中创建快捷方式。

请看一下文档,我想这就解释了为什么速度慢:https://pyinstaller.readthedocs.io/en/stable/operating-mode.html#how-the-one-file-program-works

简而言之,一个完整的程序环境需要被提取并写入一个临时文件夹。

此外,one file选项与您期望的相反:https://pyinstaller.readthedocs.io/en/stable/operating-mode.html#bundling-to-one-file

相关问题 更多 >