4

在 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

[...或 QtCore 或或。]

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 扩展已成功安装。

4

2 回答 2

2

解决方法。这有效:

# 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。

于 2012-07-09T16:08:07.280 回答
0

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

于 2021-06-08T08:50:57.167 回答