我在做什么:
我正在嘗試運行一個簡單的 tts 程式:
import pyttsx3
*** engine = pyttsx3.init()
engine.setProperty("rate", 178)
engine.say("I am the text spoken after changing the speech rate.")
engine.runAndWait()
但我在 *** 行收到以下錯誤:
Exception has occurred: ModuleNotFoundError No module named 'win32api'
我試過的:
- 上網查了一下,發現應該重新安裝pywin32(pip install pywin32),我也是這樣做的,但是這個問題還是出現了。
- 我也嘗試過直接在程式中匯入 win32api,但我仍然得到同樣的錯誤,就在現在的匯入行(***):
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()
更奇怪的是 Visual Studio Code 看到 win32api(我的 IDE 上這個詞是綠色的),但它仍然說它不存在?:

我真的很困惑,所以絕對任何幫助將不勝感激。謝謝你。
uj5u.com熱心網友回復:
這是因為 vscode 搜索模塊使用作業空間作為根目錄進行搜索,而不是當前檔案。
您需要在檔案開頭添加要匯入的檔案的相對路徑或絕對路徑。
import sys
sys.path.append("your path of module")
uj5u.com熱心網友回復:
通過附加模塊所在的路徑來解決它。不知道你能做到這一點,現在我覺得非常愚蠢。希望這可以在未來為某人節省幾個小時 lmao。
我的作業代碼是:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/485156.html
標籤:Python 视觉工作室代码 温纳皮 pywin32 pyttsx3
