代码之家  ›  专栏  ›  技术社区  ›  Ali Shefaee

pyinstaller生成的文件为外部模块提供未找到的模块错误

  •  0
  • Ali Shefaee  · 技术社区  · 2 年前

    我看到以前有人问过类似的问题,但都不适合我。 我成功地构建了我的项目,但当我运行它时,我会收到以下错误:

    Traceback (most recent call last):
      File "main.py", line 2, in <module>
    ModuleNotFoundError: No module named 'PyQt5'
    [9890] Failed to execute script 'main' due to unhandled exception!
    
    

    显然pyinstaller对外部modelus有问题。

    我尝试过的:

    • 我尝试过这个选项: --hidden-import=PyQt5
    • 我删除了 venv pyinstaller 并重新安装。
    python3 = 3.8.10
    pyinstaller = 6.0.0
    
    

    我的代码:
    main.py

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
    
    def main():
        app = QApplication(sys.argv)
        window = QMainWindow()
        label = QLabel("Hello, PyQt5!")
        window.setCentralWidget(label)
        window.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main()
    
    

    build.sh

    #!/bin/bash
    
    rm -rf dist build
    
    source venv/bin/activate
    pip3 install -r requirements.txt
    
    pyinstaller --onefile main.py
    
    deactivate
    
    ./dist/main
    

    requirements.txt

    PyQt5==5.15.2
    PyQt5-sip==12.8.1
    

    main.spec:(自动生成)

    # -*- mode: python ; coding: utf-8 -*-
    
    
    a = Analysis(
        ['main.py'],
        pathex=[],
        binaries=[],
        datas=[],
        hiddenimports=['PyQt5'],
        hookspath=[],
        hooksconfig={},
        runtime_hooks=[],
        excludes=[],
        noarchive=False,
    )
    pyz = PYZ(a.pure)
    
    exe = EXE(
        pyz,
        a.scripts,
        [],
        exclude_binaries=True,
        name='main',
        debug=False,
        bootloader_ignore_signals=False,
        strip=False,
        upx=True,
        console=True,
        disable_windowed_traceback=False,
        argv_emulation=False,
        target_arch=None,
        codesign_identity=None,
        entitlements_file=None,
    )
    coll = COLLECT(
        exe,
        a.binaries,
        a.datas,
        strip=False,
        upx=True,
        upx_exclude=[],
        name='main',
    )
    
    
    0 回复  |  直到 2 年前