使用neuralintents库时TensorFlow版本兼容问题求助
问题描述
我正在开发一款语音助手,此前从未使用过TensorFlow。我通过以下命令安装了TensorFlow:
pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl
但运行代码时出现了如下报错:
File "c:\Users\User\OneDrive\Bureau\Alexa\main.py", line 7, in <module> from neuralintents import GenericAssistant File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\neuralintents\__init__.py", line 1, in <module> from neuralintents.main import GenericAssistant File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\neuralintents\main.py", line 14, in <module> from tensorflow.keras.models import Sequential File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\tensorflow\__init__.py", line 24, in <module> from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\tensorflow\python\__init__.py", line 49, in <module> from tensorflow.python import pywrap_tensorflow File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module> from tensorflow.python.pywrap_tensorflow_internal import * File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 114 def TFE_ContextOptionsSetAsync(arg1, async):
我的语音助手代码如下:
import sys import threading import tkinter as tk import speech_recognition import pyttsx3 as tts from neuralintents import GenericAssistant class Assistant: def __init__(self): self.recognizer = speech_recognition.Recognizer() self.speaker = tts.init() self.speaker.setProperty("rate", 150) self.assistant = GenericAssistant("intents.json", intent_methods={"file": self.create_file}) threading.Thread(target=self.run_assitnat).start() def create_file(self): with open("somefile.txt", "w") as f: f.write("Hellow world") def run_assistant(self): while True: try: with speech_recognition.Microphone() as mic: self.recognizer.adjust_for_ambient_noise(mic, duration=0.2) audio = self.recognizer.listen(mic) text = self.recognizer.recognize_google(audio) text = text.lower() if "hey jake" in text: audio = self.recognizer.listen(mic) text = self.recognizer.recognize_google(audio) text = text.lower() if text == "stop": self.speaker.say("Bye") self.speaker.runAndWait() self.speaker.stop() sys.exit() else: if text is not None: response = self.assistant.request(text) if response is not None: self.speaker.say(response) self.speaker.runAndWait() except:continue Assistant()
解决方案
核心问题
- 版本严重不兼容:你安装的TensorFlow 1.8.0是2018年的旧版本,完全不支持Python 3.11。报错中的
async是Python 3.7+的关键字,旧版TensorFlow用它当参数名导致语法错误。 - 依赖路径不匹配:
neuralintents依赖TensorFlow 2.x的tensorflow.keras模块,而TensorFlow 1.x的Keras是独立的,路径不对应。
修复步骤
- 卸载旧版TensorFlow
执行命令:
pip uninstall tensorflow -y
- 安装兼容的TensorFlow版本
Python 3.11支持TensorFlow 2.13.0及以上版本,执行:
pip install tensorflow>=2.13.0
- 修正代码中的拼写错误
代码中__init__方法里的线程目标写错了方法名:run_assitnat应为run_assistant,修正后代码行:
threading.Thread(target=self.run_assistant).start()
- 更新其他依赖
确保所有相关库都是兼容版本:
pip install --upgrade speechrecognition pyttsx3 neuralintents
额外提示
- 确认
intents.json文件和代码在同一目录,格式符合neuralintents要求(需包含intents数组,每个intent有tag、patterns、responses字段)。 - 如果安装时网络卡顿,可使用国内镜像源加速:
pip install tensorflow>=2.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
内容的提问来源于stack exchange,提问作者user20886487
相关产品推荐
相关产品推荐

