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

PyInstaller打包含speedtest-cli程序报__builtin__模块缺失错误咨询

问题原因
  • 第一个报错ModuleNotFoundError: No module named '__builtin__'是旧版speedtest-cli没有完全适配Python3,仍保留了Python2专属的__builtin__模块引用
  • 第二个报错AttributeError: 'NullWriter' object has no attribute 'fileno'是因为PyInstaller使用-w/--noconsole参数打包时,会将标准输出/标准错误重定向到无fileno属性的NullWriter对象,而speedtest-cli初始化时会主动调用标准输出的fileno方法,触发属性缺失错误
修复方案

方案1:调整PyInstaller打包参数(操作最简单)

如果你的程序不需要隐藏控制台窗口,打包时去掉-w/--noconsole参数,保留控制台输出即可直接解决该问题,打包命令示例:

pyinstaller -F hw-assesment-tool.py

方案2:修改speedtest-cli源码(兼容无控制台窗口场景)

如果你必须使用无控制台窗口的打包方式,按以下步骤修改:

  1. 找到Python环境中安装的speedtest.py文件,路径一般为Lib/site-packages/speedtest.py
  2. 修复__builtin__引用错误:找到行import __builtin__,替换为
try:
    import __builtin__
except ImportError:
    import builtins as __builtin__
  1. 修复fileno属性缺失错误:找到speedtest.py中调用sys.stdout.fileno()的代码段,替换为以下逻辑,优先判断fileno是否存在,不存在时返回默认值:
try:
    fileno = sys.stdout.fileno()
except (AttributeError, IOError):
    fileno = 1 # 标准输出的默认文件描述符

修改完成后重新打包即可正常运行。

方案3:替换测速依赖

如果不想修改第三方源码,可以将speedtest-cli替换为官方维护的speedtest库(注意区别于旧版的speedtest-cli),适配性更好,打包时不会出现上述兼容问题:

pip uninstall speedtest-cli
pip install speedtest

调整代码中对应的调用逻辑后重新打包即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 00:48:02