使用 PyInstaller 构建 onefile 可执行文件后没有名为“gi”的模块

问题描述 投票:0回答:1

我正在尝试将用 Python 编写的脚本构建为单个文件可执行文件。当我在计算机上使用

pyinstaller -F script.py
时,效果很好。但是当我尝试通过 GitHub Workflows 构建可执行文件时,下载生成的二进制文件后我得到:

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!

pydbus
需要
gi
(GObject)才能工作,但是我检查了工作流程是否已安装,并且
apt
返回给我,
python3-gi
软件包已安装并且是最新的。

在构建 ofc 之前,我升级 pip,从

requirements.txt
安装依赖项并安装
pyinstaller
本身。

这是我的工作流程片段:

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 dependencies pyinstaller dbus
1个回答
0
投票

发生这种情况是因为 PyInstaller 在捆绑 Python 脚本时不会自动包含系统安装的库。您可以使用

--hidden-import
选项显式添加它,因此您的运行命令将是:

run: pyinstaller -F --hidden-import=gi script.py

或者你可以使用自定义钩子

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.