为什么我转换为exe后得到pyttsx3错误?

时间:2017-12-27 08:55:36

标签: python speech-recognition executable pyinstaller pyttsx

当我使用这个在python中运行的文件时,它会运行无限长的时间,当我使用pyinstaller将其转换为可执行文件时,它只会眨眼间运行,它会运行并切断这么快我几乎没有可以看到它。你能给我一个解释和解决方案吗?

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脚本表单中它正常运行所以我觉得在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')

1 个答案:

答案 0 :(得分:1)

您可以使用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