You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行pytest时遇AttributeError:_UnixSelectorEventLoop无_signal_handlers属性

Mac M1 PyCharm启动pytest触发asyncio AttributeError问题

错误信息

Connected to pydev debugger (build 233.15026.15)
Exception ignored in: <function BaseEventLoop.__del__ at 0x12809be50>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 688, in __del__
    self.close()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 60, in close
    for sig in list(self._signal_handlers):
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'
Exception ignored in: <function BaseEventLoop.__del__ at 0x12809be50>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 688, in __del__
    self.close()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 60, in close
    for sig in list(self._signal_handlers):
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'
Launching pytest with arguments <args here>

解决方案

  • 升级PyCharm:该错误源于PyCharm调试器(pydev)与M1架构下Python 3.9 asyncio的兼容性问题,更新至2024.x及以上版本的PyCharm可直接修复。
  • 更换Python解释器:
    1. 打开File > Settings > Project: [项目名] > Python Interpreter
    2. 替换为系统自带Python或通过conda创建适配M1的虚拟环境,避免使用Homebrew安装的Python 3.9(部分版本存在asyncio底层bug)
  • 调整调试器配置:
    1. 进入Run > Edit Configurations
    2. 选中你的pytest配置,在Configuration页勾选Emulate terminal in output console
    3. 或添加环境变量PYDEVD_DISABLE_ASYNCIO_SUPPORT=1到配置的Environment variables中
  • 手动初始化事件循环属性:在项目根目录的conftest.py中添加以下代码,提前初始化缺失的属性:
import asyncio
from asyncio import UnixSelectorEventLoop

def pytest_configure(config):
    loop = UnixSelectorEventLoop()
    if not hasattr(loop, '_signal_handlers'):
        loop._signal_handlers = {}
    asyncio.set_event_loop(loop)

内容的提问来源于stack exchange,提问作者prabodhprakash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 10:20:18