pythonspeech-recognitionexecutablepyinstallerpyttsx

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


When I use this file running in python , it runs for an infinite amount of time, when I convert it into a executable using pyinstaller , it only runs for a blink of an eye , it runs and cuts off so fast i barely can see it. Can you give me an explanation and a solution?

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()

Is there a method to looping through or is it that the voice has to be active in order for me to be able to use the program on a loop? Because the only loop is the voice recognizer? Give me your thoughts

NOTE: i get this error code when i try to run the executable file , but in py script form it runs normally So i feel something corrupts in the exe transformation

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

here is the .spec file as requested:

# -*- 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')

Solution

  • You can use collect_submodules from PyInstaller module to bundle all submodules of pyttsx3 to the distribution. Try the following spec-file:

    # -*- 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')
    

    Build by running PyInstaller against this spec file: pyinstaller testfile.spec