Windows7+Python3.6环境下Twisted与Kivy兼容问题求助
解决Python 3.6 + Windows 7下Kivy与Twisted兼容的
NotImplementedError问题 我之前在Windows 7、Python 3.6环境里搭配最新版Twisted和Kivy跑官网《Integrating with Other Frameworks》示例时,也碰到过完全一样的错误:
File "C:\Anaconda3\lib\site-packages\kivy\support.py", line 172, in install_twisted_reactor "_threadselect interface of twisted is not available on Python 3." NotImplementedError: _threadselect interface of twisted is not available on Python 3.
核心原因是Twisted在Python 3版本里已经移除了_threadselect这个旧接口,但Kivy的support.py里的 reactor 初始化逻辑还在尝试调用它。下面给你两个可行的临时解决办法:
办法一:手动修改Kivy的support.py文件
这是一劳永逸的方式,直接修正Kivy的适配逻辑:
- 找到报错里的文件路径:
C:\Anaconda3\lib\site-packages\kivy\support.py - 用文本编辑器打开它,找到
install_twisted_reactor函数 - 把原来的这段代码:
替换成:try: from twisted.internet import _threadselect reactor = _threadselect.install() except ImportError: raise NotImplementedError("_threadselect interface of twisted is not available on Python 3.")# 跳过_threadselect检查,直接使用Python3兼容的asyncio reactor from twisted.internet import asyncioreactor asyncioreactor.install() from twisted.internet import reactor - 保存文件后再运行你的代码,就能正常启动reactor了。
办法二:在自己的代码中提前指定兼容的reactor
如果不想修改第三方库的源码,可以在你的项目代码最开头添加以下代码,手动初始化Twisted的Python3兼容reactor:
# 先安装Twisted的asyncio reactor import twisted.internet.asyncioreactor twisted.internet.asyncioreactor.install() # 之后再导入Kivy和Twisted的相关模块 from twisted.internet import reactor from kivy.app import App # ... 你的其他业务代码
这样就能绕过Kivy自带的那个有问题的reactor安装逻辑,直接使用支持Python3的reactor实现。
内容的提问来源于stack exchange,提问作者The Guy
相关产品推荐
相关产品推荐

