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

使用Pytest运行Python测试时出现console_main导入错误

解决pytest导入console_main失败的问题

问题原因

console_main在pytest 7.0及以上版本中已被移除,你的启动脚本仍在尝试导入这个已废弃的入口函数,从而触发ImportError。另外也可能是虚拟环境中的pytest安装文件损坏。

解决方案

方案1:修改启动脚本适配新版pytest

打开/tmp/func/bin/pytest文件,将代码替换为:

#!/tmp/func/bin/python3.8
# -*- coding: utf-8 -*-
import re
import sys
import pytest
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(pytest.main())

方案2:重新安装pytest修复损坏文件

如果该脚本是pytest自动生成的,说明安装可能存在异常,执行以下命令重装:

pip uninstall pytest -y
pip install pytest

方案3:使用官方推荐的启动方式

跳过当前有问题的脚本,直接在终端运行:

python -m pytest

这是pytest官方推荐的启动方式,能直接避免脚本兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 11:34:53