Search code examples
pythonvisual-studio-codewinapipywin32pyttsx3

Exception has occurred: ModuleNotFoundError No module named 'win32api'


WHAT I'M DOING:

I'm trying to run a simple tts program:

import pyttsx3

*** engine = pyttsx3.init()
engine.setProperty("rate", 178)
engine.say("I am the text spoken after changing the speech rate.")
engine.runAndWait()

but I'm getting the following error at the *** line:

Exception has occurred: ModuleNotFoundError No module named 'win32api'

WHAT I'VE TRIED:

  • I looked online and found that I should re-install pywin32 (pip install pywin32), which I did, but this problem is still here.
  • I've also tried importing win32api directly in the program, but I still get the same error, just at the import line now (***):
import pyttsx3
*** import win32api

engine = pyttsx3.init()
engine.setProperty("rate", 178)
engine.say("I am the text spoken after changing the speech rate.")
engine.runAndWait()

Even weirder is Visual Studio Code sees win32api (the word is green on my IDE), but it still says it doesn't exist?:

enter image description here

I'm really confused so absolutely any help would be greatly appreciated. Thank you.


Solution

  • This is because the vscode search module using the workspace as the root directory to search, rather than the current file.

    You need to add the relative path or absolute path of the file to be imported at the beginning of the file.

    import sys
    sys.path.append("your path of module")