为什么在转换为 exe 后出现 pyttsx3 错误?

why am i getting a pyttsx3 error after converting to exe?

当我在 python 中使用此文件 运行ning 时,它 运行s 无限长,当我使用 pyinstaller 将其转换为可执行文件时,它仅 运行s 一眨眼,它 运行s 并且切断得如此之快,我几乎看不到它。你能给我一个解释和解决方案吗?

import speech_recognition as sr
import pyttsx3
import pocketsphinx
import pyaudio
import random
import os
import wikipedia
import time





engine = pyttsx3.init()
def talktome(text):
    engine.say(text)
    engine.runAndWait()
#default start up
talktome('Caios is now online ronald,sir')
talktome('how can i assist you')


#main function contaning all commands 
def mainfunction():
    a=r.listen(source)
    user= r.recognize_sphinx(a)
    print(user)


                       #main commands

    numb_of_times = 0

    #greetings recognition       
    if user == 'hello' or user  == 'wassup' or user == 'hi' or user == 'hows it going':
        numb_of_times +=1
        #checking to see if the number of times greeted is more than 2
        #adding a little personality to caios
        if numb_of_times > 2 :
            z='still here sir' , 'how many times are you going to greet me sir'
            n = random.choice(z)
            talktome(n)


        a = 'Hi ,Sir how are you doing today?' , 'how is it going ,sir'
        k = random.choice(a)

        talktome(k)
    #Unpleasant greetings recognition
    elif user == 'bitch' or user == 'whore' or user  == 'hoe' or user == 'slut' or user == 'pussy':
     k = 'just a tip , Caios doesnt respond to ignorace', 'thats not nice , do you talk to your parents with that tone'
     A= random.choice(k)
     talktome(A)





    else:
        print('unknown command of C.A.I.O.S')
        print('                                ')




#speech recognition function/if statement    -
if __name__ == "__main__":                         
    r = sr.Recognizer()
    with sr.Microphone() as source:
        while 3:
            mainfunction()

是否有循环的方法,或者是否必须激活语音才能让我能够循环使用该程序?因为唯一的循环是语音识别器?说出你的想法

注意:当我尝试 运行 可执行文件时收到此错误代码,但在 py 脚本中它通常 运行s 所以我觉得在 exe 转换中有些东西损坏

file.exe                                                                        

Traceback (most recent call last):
File "site-packages\pyttsx3\__init__.py", line 44, in init
File "c:\users\kxrk\appdata\local\programs\python\python36-32\lib\weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
        File "testfile.py", line 14, in <module>
        File "site-packages\pyttsx3\__init__.py", line 46, in init
        File "site-packages\pyttsx3\engine.py", line 52, in __init__
        File "site-packages\pyttsx3\driver.py", line 75, in __init__        
        File "importlib\__init__.py", line 126, in import_module
        File "<frozen importlib._bootstrap>", line 994, in _gcd_import
        File "<frozen importlib._bootstrap>", line 971, in _find_and_load
        File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
        File "<frozen importlib._bootstrap>", line 994, in _gcd_import
        File "<frozen importlib._bootstrap>", line 971, in _find_and_load
        File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
    ModuleNotFoundError: No module named 'pyttsx3.drivers'
[2768] Failed to execute script testfile

这里是请求的 .spec 文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['testfile.py'],
             pathex=['C:\Users\Kxrk\AppData\Local\Programs\Python\Python36-32\Scripts'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='testfile',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='testfile')

您可以使用 PyInstaller 模块中的 collect_submodulespyttsx3 的所有子模块捆绑到发行版中。尝试以下规范文件:

# -*- mode: python -*-
from PyInstaller.utils.hooks import collect_submodules


block_cipher = None
hidden_imports = collect_submodules('pyttsx3')


a = Analysis(['testfile.py'],
             pathex=['C:\Users\Kxrk\AppData\Local\Programs\Python\Python36-32\Scripts'],
             binaries=[],
             datas=[],
             hiddenimports=hidden_imports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='sr',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='sr')

通过 运行 PyInstaller 针对此规范文件构建 pyinstaller testfile.spec