我正在创建我的 python 文件的 exe 文件,但它总是崩溃。但它作为 python 文件工作正常

I am creating the exe file of my python file but it always crash. but it is working fine as python file

我正在使用 pyinstaller 创建 Exe 文件,它没有显示任何错误,但是当我执行 exe 文件时它总是崩溃。但它作为 python 文件工作正常,请帮助。

这里是exe文件转换的Traceback。

    (aiEnv) PS E:\AI Creation> pyinstaller check.py
    751 INFO: PyInstaller: 3.5
    751 INFO: Python: 3.7.4
    752 INFO: Platform: Windows-10-10.0.18362-SP0
    785 INFO: wrote E:\AI Creation\check.spec
    786 INFO: UPX is not available.
    803 INFO: Extending PYTHONPATH with paths
    ['E:\AI Creation', 'E:\AI Creation']
    803 INFO: checking Analysis
    803 INFO: Building Analysis because Analysis-00.toc is non existent
    803 INFO: Initializing module dependency graph...
    809 INFO: Initializing module graph hooks...
    885 INFO: Analyzing base_library.zip ...
    6922 INFO: running Analysis Analysis-00.toc
    6940 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
      required by e:\ai creation\aienv\scripts\python.exe
    8121 INFO: Caching module hooks...
    8598 INFO: Analyzing E:\AI Creation\check.py
    9470 INFO: Processing pre-find module path hook   distutils
    9530 INFO: distutils: retargeting to non-venv dir 'c:\users\karti\appdata\local\programs\python\python37\Lib\distutils\__init__.py'
    9629 INFO: Processing pre-safe import module hook   six.moves
    17450 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
    19901 INFO: Processing pre-find module path hook   site
    19965 INFO: site: retargeting to fake-dir 'e:\ai creation\aienv\lib\site-packages\PyInstaller\fake-modules'
    21231 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
    25426 INFO: Loading module hooks...
    25427 INFO: Loading module hook "hook-certifi.py"...
    25441 INFO: Loading module hook "hook-encodings.py"...
    25540 INFO: Loading module hook "hook-httplib2.py"...
    25550 INFO: Loading module hook "hook-pkg_resources.py"...
    26110 INFO: Processing pre-safe import module hook   win32com
    26247 INFO: Loading module hook "hook-pycparser.py"...
    26275 INFO: Loading module hook "hook-pydoc.py"...
    26276 INFO: Loading module hook "hook-pythoncom.py"...
    26634 INFO: Loading module hook "hook-pywintypes.py"...
    26992 INFO: Loading module hook "hook-selenium.py"...
    27068 INFO: Loading module hook "hook-setuptools.py"...
    27409 WARNING: Hidden import "distutils.command.build_ext" not found!
    31666 INFO: Loading module hook "hook-speech_recognition.py"...
    31810 INFO: Loading module hook "hook-sysconfig.py"...
    31828 INFO: Loading module hook "hook-unidecode.py"...
    33771 INFO: Loading module hook "hook-win32com.py"...
    34846 INFO: Loading module hook "hook-xml.dom.domreg.py"...
    34852 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
    34853 INFO: Loading module hook "hook-xml.py"...
    34854 INFO: Loading module hook "hook-_tkinter.py"...
    35296 INFO: checking Tree
    35296 INFO: Building Tree because Tree-00.toc is non existent
    35298 INFO: Building Tree Tree-00.toc
    35418 INFO: checking Tree
    35418 INFO: Building Tree because Tree-01.toc is non existent
    35420 INFO: Building Tree Tree-01.toc
    35436 INFO: Loading module hook "hook-numpy.core.py"...
    37229 INFO: Loading module hook "hook-numpy.py"...
    37305 INFO: Looking for ctypes DLLs
    37353 INFO: Analyzing run-time hooks ...
    37361 INFO: Including run-time hook 'pyi_rth_certifi.py'
    37379 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
    37413 INFO: Including run-time hook 'pyi_rth_pkgres.py'
    37420 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
    37428 INFO: Including run-time hook 'pyi_rth__tkinter.py'
    37449 INFO: Looking for dynamic libraries
    47405 INFO: Looking for eggs
    47406 INFO: Using Python library e:\ai creation\aienv\scripts\python37.dll
    47406 INFO: Found binding redirects:
    []
    47418 INFO: Warnings written to E:\AI Creation\build\check\warn-check.txt
    47568 INFO: Graph cross-reference written to E:\AI Creation\build\check\xref-check.html
    47701 INFO: checking PYZ
    47702 INFO: Building PYZ because PYZ-00.toc is non existent
    47703 INFO: Building PYZ (ZlibArchive) E:\AI Creation\build\check\PYZ-00.pyz
    49935 INFO: Building PYZ (ZlibArchive) E:\AI Creation\build\check\PYZ-00.pyz completed successfully.
    49968 INFO: checking PKG
    49969 INFO: Building PKG because PKG-00.toc is non existent
    49970 INFO: Building PKG (CArchive) PKG-00.pkg
    50049 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
    50051 INFO: Bootloader e:\ai creation\aienv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
    50052 INFO: checking EXE
    50053 INFO: Building EXE because EXE-00.toc is non existent
    50056 INFO: Building EXE from EXE-00.toc
    50101 INFO: Appending archive to EXE E:\AI Creation\build\check\check.exe
    50187 INFO: Building EXE from EXE-00.toc completed successfully.
    50203 INFO: checking COLLECT
    50203 INFO: Building COLLECT because COLLECT-00.toc is non existent
    50205 INFO: Building COLLECT COLLECT-00.toc
    61880 INFO: Building COLLECT COLLECT-00.toc completed successfully.

这是我正在转换成 exe 文件的文件的代码,如果有任何错误或我遗漏了什么,请告诉我。我在这里使用 pyttsx3 库、语音识别库、日期时间库、维基百科库、网络浏览器库、os 库和 smtplib。

这段代码的工作很简单,在这段代码中,我会说一些话,虚拟助手会根据我所说的进行回复,我正在使用 windows 内置语音,我的 [=25] 中有 4 个语音=].

pyaudio有没有问题或者windows语音有什么问题请解释。

import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia #pip install wikipedia
import webbrowser
import os
import smtplib

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices)
engine.setProperty('rate', 135)     # setting up new voice rate
engine.setProperty('voice', voices[2].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning Sir,")

    elif hour>=12 and hour<18:
        speak("Good Afternoon Sir,")   

    else:
        speak("Good Evening Sir,")  

    speak(" My name is ashs")       

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 0.8
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)    
        print("Say that again please...")  
        return "None"
    return query

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('youremail@gmail.com', 'your-password')
    server.sendmail('youremail@gmail.com', to, content)
    server.close()

if __name__ == "__main__":
    wishMe()
    while True:
    # if 1:
        query = takeCommand().lower()
        # Logic for executing tasks based on query
        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=2)
            speak("According to Wikipedia")
            print(results)
            speak(results)

        elif 'open youtube' in query:
            webbrowser.open("youtube.com")

        elif 'who created you' in query:
            speak("Kartik chauhan, created me on 7 august 2019")

        elif 'what is your age' in query:
            speak("Shut up! i cant tell you my age. dont ask me again")

        elif  'how old are you' in query:
            speak("Shut up! i cant tell you my age. dont ask me again")

        elif 'how are you' in query:
            speak("I am fine , what about you")

        elif 'i am good' in query:
            speak("nice! whats going on? can i help you?")

        elif 'pooja' in query:
            speak("you called me?")
            query = takeCommand().lower()
            if 'no' in query:
                speak("thanks for using me")
                exit
            elif 'yes' in query:
                speak("Ask me a question")
                exit
            else:
                speak('i cant  understand you.')
        elif 'open google' in query:
            webbrowser.open("google.com")

        elif 'open Whosebug' in query:
            webbrowser.open("whosebug.com")  

        elif 'open facebook' in query:
            webbrowser.open("fb.com") 

        elif 'open Chrome' in query:
            music_dir = 'C:\Program Files (x86)\Google\Chrome\Application'
            if os.path.isdir("music_dir"):
                if len(music_dir) == 0:
                    speak("Sorry, But i cant find any music")
                else:   
                    if os.startfile(os.path.join(music_dir,'chrome.exe')):
                        speak("Here is your google chrome")
                    else:
                        speak("Cant open your browser")
            else:
                speak("There is no such directory")

        elif 'play music' in query:
            music_dir = 'D:\Non Critical\songs\Favorite Songs2'
            if os.path.isdir("music_dir"):
                if len(music_dir) == 0:
                    speak("Sorry, But i cant find any music")
                else:
                    songs = os.listdir(music_dir)
                    print(songs)    
                    os.startfile(os.path.join(music_dir, songs[0]))
            else:
                speak("There is no such directory")

        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")    
            speak(f"Sir, the time is {strTime}")

        elif 'open code' in query:
            codePath = "C:\Users\Haris\AppData\Local\Programs\Microsoft VS Code\Code.exe"
            os.startfile(codePath)

        elif 'email to harry' in query:
            try:
                speak("What should I say?")
                content = takeCommand()
                to = "harryyourEmail@gmail.com"    
                sendEmail(to, content)
                speak("Email has been sent!")
            except Exception as e:
                print(e)
                speak("Sorry my friend harry bhai. I am not able to send this email")
        elif 'ok fine' in query:
            speak("Can i assist you in anything else?")
            query = takeCommand().lower()
            if 'no' in query:
                speak("thanks for using me")
                exit
            elif 'yes' in query:
                speak("Ask me a question")
                exit

代替pyinstaller check.py,使用:

pyinstaller --hidden-import=pyttsx3.drivers --hidden-import=pyttsx3.drivers.dummy \
   --hidden-import=pyttsx3.drivers.espeak --hidden-import=pyttsx3.drivers.nsss \
   --hidden-import=pyttsx3.drivers.sapi5 --onefile check.py

我遇到了同样的问题。您必须升级到 pyinstaller 版本 4.8 然后使用命令:pyinstaller --hidden-import=pyaudio --onefile -w check.py 查看 this post 了解更多详情。