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

PyInstaller打包exe遇ModuleNotFoundError问题求助

解决PyInstaller打包时sortedcontainers/twitchio模块找不到的问题

问题概述

使用PyInstaller打包Python Twitch机器人项目为.exe时,即使已通过pip安装依赖并添加--hidden-import参数,仍触发ModuleNotFoundError: No module named 'sortedcontainers'(后续会触发twitchio相关错误)。项目依赖已列在requirements.txt,尝试过多种打包命令均无效。

相关信息

  1. 报错文件导入代码(twitch_bot.py):
from sortedcontainers import SortedList
from twitchio.ext import commands
from queue import Empty
from PIL import Image
import datetime as dt
import aiohttp
import requests
import asyncio
import json
import io
import os
import twitchio.errors
  1. 运行exe时错误信息:
Traceback (most recent call last):
  File "main.py", line 1, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module
  File "twitch_bot.py", line 1, in <module>
ModuleNotFoundError: No module named 'sortedcontainers'
  1. requirements.txt内容:
customtkinter~=5.2.2
aiohttp~=3.12.13
requests~=2.32.4
twitchio~=2.10.0
sortedcontainers~=2.4.0
pillow~=11.3.0
  1. 尝试过的打包命令:
pyinstaller --clean --noconsole -F --name "Twitch Chatter Catcher" main.py

pyinstaller --clean --noconsole -F --name "Twitch Chatter Catcher" main.py --hidden-import=sortedcontainers --hidden-import=twitchio

pyinstaller --clean --noconsole -F --name "Twitch Chatter Catcher" main.py --hidden-import sortedcontainers

pyinstaller --noconsole -F --name "Twitch Chatter Catcher" main.py
  1. 项目结构:
Project/
├─ requirements.txt
├─ setup/
│  ├─ setup_gui.py
├─ themes/
│  ├─ purple_twitch.json
├─ gui.py
├─ main.py
├─ README.md
├─ twitch_bot.py

解决思路

1. 确认虚拟环境一致性

  • 确保打包时使用的Python环境和安装依赖的环境完全一致:
    • 运行where python(Windows)或which python(Linux/macOS)查看当前Python路径;
    • 运行pip list检查sortedcontainers和twitchio是否确实安装在该环境中;
    • 如果使用虚拟环境,必须先激活环境再执行打包命令。

2. 扩展hidden-import的覆盖范围

PyInstaller的hidden-import可能需要更具体的子模块,尤其是twitchio这类嵌套结构复杂的库:

# Windows环境用^换行,Linux/macOS用\
pyinstaller --clean --noconsole -F --name "Twitch Chatter Catcher" main.py ^
--hidden-import=sortedcontainers ^
--hidden-import=twitchio ^
--hidden-import=twitchio.ext.commands ^
--hidden-import=twitchio.errors

3. 使用.spec文件手动指定依赖

生成.spec文件后手动编辑,能更精准控制打包内容:

  1. 生成初始.spec文件:
    pyinstaller --name "Twitch Chatter Catcher" main.py
    
  2. 打开生成的Twitch Chatter Catcher.spec,在hiddenimports列表中添加所有缺失的模块:
    hiddenimports=['sortedcontainers', 'twitchio', 'twitchio.ext.commands', 'twitchio.errors'],
    
  3. 用.spec文件重新打包:
    pyinstaller --clean --noconsole -F "Twitch Chatter Catcher.spec"
    

4. 升级PyInstaller版本

旧版本PyInstaller可能对部分库的支持不完善,尝试升级到最新版:

pip install --upgrade pyinstaller

5. 强制指定依赖路径(极端情况)

如果模块安装路径特殊,可通过--paths参数直接指定site-packages目录:

# 替换为你的虚拟环境site-packages实际路径
pyinstaller --clean --noconsole -F --name "Twitch Chatter Catcher" ^
--paths="C:\Your\VirtualEnv\Lib\site-packages" ^
--hidden-import=sortedcontainers --hidden-import=twitchio main.py

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:12:44