Python 简单教程变量未定义问题
Python simple tutorial variable not defined issue
所以我在 youtube 上关注了一个简单的教程,但无论我做什么,我总是遇到同样的问题。
这是我使用的代码。
import speech_recognition as sr
import pyttsx3
voices = []
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print(voice.id)
我正在用 sublimeText3 写这个。每次我构建这个,我都会得到同样的错误。
File "C:\Users\This PC\Desktop\Py\introTest.py", line 14, in
voices = engine.getProperty('voices') NameError: name 'engine' is not defined
不确定为什么说 "engine" 未定义。我明明定义了下试试。任何帮助将不胜感激。
删除 try/excepts 后我有很多新错误。这是构建日志。
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]
尝试在try/catch块中输入导入,并检查导入后是否未定义,因此抛出异常。
查看模块官方网站,查看您的控制台日志以查找其中的一些错误,
修复了可能的错误:
No module named win32com.client
No module named win32
No module named win32api
试试这个
pip install pypiwin32
另外显示您的日志,以便我们更好地了解情况!
看到完整的错误日志转储(没有 try
块隐藏错误)后,最后一位是文本:
line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]
我建议查看 。 (提示:它告诉你你缺少 pyttsx3
依赖的模块)
您正在尝试分配 engine = pyttsx3.init()
,但 when/if 失败,然后您声明 voices = engine.getProperty('voices')
。但由于 try
块失败,engine
尚未声明。
下面,我分配 engine = None
,并跳过 try
块中的 else
;而是使用条件来确定它是否为 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")
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)
在 Ubuntu 我遇到了同样的问题。有史以来最简单的修复:
sudo apt install libespeak1
嗨,我遇到过类似的问题,这就是我如何轻松改变声音的方法
import pyttsx3
voices = []
eng = pyttsx3.init()
voices = eng.getProperty('voice')
eng.setProperty('voice',voices[1].id)
eng.say("Some text here")
eng.runAndWait()
所以我在 youtube 上关注了一个简单的教程,但无论我做什么,我总是遇到同样的问题。
这是我使用的代码。
import speech_recognition as sr
import pyttsx3
voices = []
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print(voice.id)
我正在用 sublimeText3 写这个。每次我构建这个,我都会得到同样的错误。
File "C:\Users\This PC\Desktop\Py\introTest.py", line 14, in voices = engine.getProperty('voices') NameError: name 'engine' is not defined
不确定为什么说 "engine" 未定义。我明明定义了下试试。任何帮助将不胜感激。
删除 try/excepts 后我有很多新错误。这是构建日志。
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]
尝试在try/catch块中输入导入,并检查导入后是否未定义,因此抛出异常。 查看模块官方网站,查看您的控制台日志以查找其中的一些错误,
修复了可能的错误:
No module named win32com.client
No module named win32
No module named win32api
试试这个
pip install pypiwin32
另外显示您的日志,以便我们更好地了解情况!
看到完整的错误日志转储(没有 try
块隐藏错误)后,最后一位是文本:
line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]
我建议查看 pyttsx3
依赖的模块)
您正在尝试分配 engine = pyttsx3.init()
,但 when/if 失败,然后您声明 voices = engine.getProperty('voices')
。但由于 try
块失败,engine
尚未声明。
下面,我分配 engine = None
,并跳过 try
块中的 else
;而是使用条件来确定它是否为 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")
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)
在 Ubuntu 我遇到了同样的问题。有史以来最简单的修复:
sudo apt install libespeak1
嗨,我遇到过类似的问题,这就是我如何轻松改变声音的方法
import pyttsx3
voices = []
eng = pyttsx3.init()
voices = eng.getProperty('voice')
eng.setProperty('voice',voices[1].id)
eng.say("Some text here")
eng.runAndWait()