GitHub Workflows中PyInstaller打包单文件报No module named 'gi'错误
解决GitHub Actions中PyInstaller打包pydbus时缺失'gi'模块的问题
问题场景
本地执行pyinstaller -F script.py打包含pydbus依赖的Python脚本时,生成的可执行文件能正常运行;但通过GitHub Workflows构建后,下载的二进制文件运行时报错找不到gi模块。已确认Workflow环境中python3-gi包已安装且为最新版本。
报错信息
Traceback (most recent call last): File "script.py", line 3, in <module> File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module File "pydbus/__init__.py", line 1, in <module> File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module File "pydbus/bus.py", line 1, in <module> ModuleNotFoundError: No module named 'gi' [PYI-11218:ERROR] Failed to execute script 'script' due to unhandled exception!
现有Workflow片段
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: 💾 Set up Python uses: actions/setup-python@v5 with: python-version: '3.x' architecture: 'x64' - name: 💾 Upgrade pip run: python -m pip install --upgrade pip - name: 💾 Install dependencies run: python -m pip install -r requirements.txt - name: 💾 Install pyinstaller run: python -m pip install -U pyinstaller - name: 🔨 Build executable run: pyinstaller -F script.py
解决方案
报错原因是Python依赖中缺失PyGObject:python3-gi是系统级包,但PyInstaller打包时需要Python环境中的PyGObject才能正确识别并打包gi模块。
修复步骤:
- 在
requirements.txt中添加PyGObject依赖:
PyGObject>=3.46.0
(版本号可根据需求调整)
- 若未使用
requirements.txt,也可直接在Workflow的依赖安装步骤中执行:
python -m pip install PyGObject
修改后重新运行GitHub Workflows构建,生成的可执行文件即可正常运行。
内容的提问来源于stack exchange,提问作者sech1p
相关产品推荐
相关产品推荐

