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

运行python manage.py shell遇OSError(WinError6)无效句柄错误求助

解决Django shell导入模型时的WinError 6错误

这个错误是Windows环境下Python 3.8与asyncio事件循环、IPython版本不兼容导致的,可通过以下几种方式解决:

  • 改用原生Python shell
    执行python manage.py shell --interface=python进入标准Python交互环境,再执行from flights.models import Flight,避开IPython的兼容性问题。

  • 降级IPython版本
    当前使用的IPython 8.12.3和Python 3.8适配性较差,降级到稳定兼容版本:

    pip install ipython==7.34.0
    

    重新运行python manage.py shell后尝试导入模型。

  • 修改asyncio事件循环策略
    在项目的settings.py文件末尾添加以下代码,强制使用兼容的事件循环:

    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    

    保存后重启shell测试。

  • 升级Python版本
    Python 3.8的Windows asyncio模块存在已知bug,升级到Python 3.9及以上版本(如3.10)可从底层解决这类兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:54:57