Python简单教程变量未定义issu

2024-04-16 13:00:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在youtube上看了一个简单的教程,不管我做什么,我总是得到同样的问题。在

这是我使用的代码。在

import speech_recognition as sr
import pyttsx3


voices = []
engine = pyttsx3.init()

voices = engine.getProperty('voices')
for voice in voices:
    print(voice.id)

我是用sublimitext3写的。每次我建立这个,我得到相同的错误。在

File "C:\Users\This PC\Desktop\Py\introTest.py", line 14, in voices = engine.getProperty('voices') NameError: name 'engine' is not defined

不知道为什么说“引擎”没有定义。我很清楚的在尝试下定义了它。任何帮助都将不胜感激。在

删除try/exceptions之后,我有很多新的错误。这是构建日志。在

Traceback (most recent call last): File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3__init__.py", line 44, in init eng = _activeEngines[driverName] File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\weakref.py", line 137, in getitem o = self.datakey KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\This PC\Desktop\Py\demo.py", line 7, in engine = pyttsx3.init() File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3__init__.py", line 46, in init eng = Engine(driverName, debug) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\engine.py", line 52, in init self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\driver.py", line 75, in init self._module = importlib.import_module(name) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\importlib__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked
File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]


Tags: inpyimportinitliblocallinethis
3条回答

尝试在Try/catch块中输入导入,并检查导入后是否未定义,因此引发异常。 在模块官方网站中,查看你的控制台日志中是否有这些错误

修复可能的错误: No module named win32com.client No module named win32 No module named win32api

试试这个

pip install pypiwin32

另外,请出示你的日志,以便我们更好地了解情况!在

import speech_recognition as sr
import pyttsx3

voices = [] 
try:
    engine = pyttsx3.init()
except ImportError:
    print('Requested driver is not found')
    engine = None
except RuntimeError:
    print('Driver fails to initialize')
    engine = None

if engine is None:
    print('Something went wrong')

else:
    voices = engine.getProperty('voices')
    for voice in voices:
        print(voice.id)

在看到完整的错误日志转储(没有try块隐藏错误)后,最后一位是文本:

line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]

我建议你看看Jefferson Puchalski's answer again。(提示:它告诉您缺少pyttsx3依赖的模块)


您试图分配engine = pyttsx3.init(),但是当/如果失败时,您就声明voices = engine.getProperty('voices')。但是由于try块失败,engine没有被声明。在

下面,我指定engine = None,并跳过else形式的try块;而是使用一个条件来确定它是否是None(它在创建engine时工作正常)。在

import speech_recognition as sr
import pyttsx3


voices = []
engine = None

try:
    engine = pyttsx3.init()
except ImportError:
    print('Import Issue')
except RuntimeError:
    print('Runtime Issue')

if (engine is not None):
    voices = engine.getProperty('voices')
    for voice in voices:
        print(voice.id)
else:
    print("Something went wrong")

相关问题 更多 >