Home > database >  Exception has occurred: ModuleNotFoundError No module named 'win32api'
Exception has occurred: ModuleNotFoundError No module named 'win32api'

Time:06-01

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.

CodePudding user response:

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

CodePudding user response:

Solved it by appending the path where the module was. Had no idea you could do this and now I feel incredibly stupid. Hopefully this saves someone in the future a few hours lmao.

My working code is:

import pyttsx3
import sys
sys.path.append(r'C:\Users\jack_l\AppData\Local\Programs\Python\Python310\Lib\site-packages\win32') #Where win32api is located
sys.path.append(r'C:\Users\jack_l\AppData\Local\Programs\Python\Python310\Lib\site-packages\win32\lib') #Where pywintypes is located (this error popped up after I appended where win32api is located)

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