【问题标题】:Pyinstaller: ImportError: cannot import name QtGuiPyinstaller:ImportError:无法导入名称 QtGui
【发布时间】:2023-03-15 10:17:01
【问题描述】:

在 Windows 7 上使用最新的pyinstaller 制作独立的 exe (-F),运行 exe 时:

ImportError: 无法导入名称 QtGui

在 pyinstaller hooks 目录中,有针对 PyQt4 的特殊处理,但不是 PySide。

希望找到解决方法或尝试其他方法。

环境
Windows 7 64 位
Python 2.7 32 位
PYTHONHOME=c:\python27
PYTHONPATH=c:\python27\lib
PYTHONLIB=c:\python27\libs\python27.lib;c:\python27\lib\site-packages

步骤
1.从http://releases.qt-project.org/pyside/1.1.1/PySide-1.1.1qt474.win32-py2.7.exe添加PySide
2.解压https://github.com/pyinstaller/pyinstaller/zipball/develop到c:\pyinstaller1.5.1
3. 对包含以下内容的 .py 文件运行以下命令:

from PySide import QtGui

[...or QtCore or or.]

运行

c:\pyinstaller1.5.1>pyinstaller.py -F import_test.py
108 INFO: wrote c:\pyinstaller1.5.1\import_test.spec
171 INFO: Testing for ability to set icons, version resources...
296 INFO: ... resource update available
312 INFO: UPX is not available.
4321 INFO: checking Analysis
4382 INFO: checking PYZ
4430 INFO: checking PKG
4446 INFO: building because c:\pyinstaller1.5.1\build\pyi.win32\import_test\import_test.exe.manifest changed
4446 INFO: building PKG out00-PKG.pkg
16782 INFO: checking EXE
16782 INFO: rebuilding out00-EXE.toc because pkg is more recent
16782 INFO: building EXE from out00-EXE.toc
16799 INFO: Appending archive to EXE c:\pyinstaller1.5.1\dist\import_test.exe

c:\pyinstaller1.5.1>dist\import_test.exe
Traceback (most recent call last):
  File "<string>", line 23, in <module>
ImportError: cannot import name QtGui

注意

在 PySide 安装结束时(以管理员身份),此消息: 在文件对象析构函数中关闭失败: sys.excepthook 丢失 丢失 sys.stderr 如果这是关于安装后的,可以手动处理: c:>python.exe c:\Python27\Scripts\pyside_postinstall.py -install 正在生成文件 C:\python27\qt.conf... PySide 安装在 c:/python27/Lib/site-packages/PySide... PySide 扩展已成功安装。

【问题讨论】:

    标签: pyside pyinstaller


    【解决方案1】:

    解决方法。这有效:

    # Various imports, whatever, using normal sys.path, for example:
    import os, sys, re, time, random
    import subprocess, psutil
    
    # Save sys.path
    sys_path_saved = sys.path
    
    # Limit sys.path for PySide import
    sys.path = ['c:\\python27\\lib\\site-packages']
    
    # PySide imports with limited sys.path
    from PySide        import QtGui, QtCore
    from PySide.QtGui  import QApplication, QLineEdit
    from PySide.QtCore import QSettings, Qt
    
    # Reset sys.path to original
    sys.path = sys_path_saved
    
    # Remainder of code...
    

    Pyinstaller 1.5.1 应该可以很好地定位依赖项,而且经常这样做。 但是,在 .spec 中使用其 pathex 或 hiddenimports 的许多尝试都失败了。 修改我的环境变量也失败了。 从 .egg 中手动提取各种模块文件有时会奏效。

    但是对于 PySide 导入,上述 sys.path 临时限制是可行的解决方法。

    更新:不幸的是,该 exe 只能在安装了 Python/Pyside 的机器上运行,不能在没有 Python 的 XP 上运行。

    【讨论】:

      【解决方案2】:

      对我来说,直接起作用的是将两个文件夹 PySide6shiboken6 从我的 python 安装文件夹 (PYTHON_FOLDER/Lib/site-packages) 复制/粘贴到由 pyinstaller SCRIPT_NAME 生成的 dist/APP_NAME 文件夹中。

      【讨论】:

        猜你喜欢
        • 2022-01-11
        • 1970-01-01
        • 2021-01-23
        • 2021-12-12
        • 2011-02-13
        • 2018-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多