使用PyInstaller打包含curses的Python项目生成exe后报'_curses'模块缺失错误
解决PyInstaller打包curses程序时出现
ModuleNotFoundError: No module named '_curses'的问题 问题场景
仅包含main.py的Python项目,代码中导入了curses模块,用PyInstaller打包成.exe后运行报错:
Traceback (most recent call last): File "main.py", line 11, in <module> File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module File "curses\__init__.py", line 13, in <module> ModuleNotFoundError: No module named '_curses' Failed to execute script 'main' due to unhandled exception!
已尝试过以下命令但均无效:
pyinstaller --onefile --hidden-import curses --additional-hooks-dir=. main.py pyinstaller --onefile --hidden-import windows-curses --additional-hooks-dir=. main.py pyinstaller.exe --onefile -w main.py
解决方案
1. 安装Windows兼容的curses库
Windows原生Python不自带curses模块,必须安装windows-curses替代:
pip install windows-curses
2. 正确配置PyInstaller打包命令
_curses是curses依赖的底层C扩展模块,PyInstaller默认无法自动检测到它,需要手动指定为隐藏导入:
pyinstaller --onefile --hidden-import curses --hidden-import _curses main.py
3. (可选)使用自定义Hook文件
如果上述命令仍无效,可创建一个PyInstaller Hook文件hook-curses.py,内容如下:
from PyInstaller.utils.hooks import collect_all datas, binaries, hiddenimports = collect_all('curses')
然后执行打包命令时指定Hook目录:
pyinstaller --onefile --additional-hooks-dir=. main.py
注意事项
- 不要使用
-w参数:curses是终端交互界面,-w会禁用控制台窗口,导致curses无法初始化,引发额外错误。
内容的提问来源于stack exchange,提问作者vilal32
相关产品推荐
相关产品推荐

