Pyinstaller:ImportError:无法导入名称QtGui

时间:2012-07-04 21:44:03

标签: pyside pyinstaller

在运行exe时,使用Windows 7上的最新pyinstaller创建一个独立的exe(-F):

  

ImportError:无法导入名称QtGui

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

希望为此尝试解决方法。

环境
Windows 7 64位
Python 2.7 32位
PYTHONHOME = C:\ python27
PYTHONPATH = C:\ python27 \ lib中
PYTHONLIB = C:\ python27 \库\ python27.lib; C:\ python27 \ lib中\站点包

步骤
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或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

注意

At the end of the PySide install (as admin), this message:
    close failed in file object destructor:
    sys.excepthook is missing
    lost sys.stderr
If that is about post install it can be handled manually:
    c:>python.exe c:\Python27\Scripts\pyside_postinstall.py -install
    Generating file C:\python27\qt.conf...
    PySide installed in c:/python27/Lib/site-packages/PySide...
    The PySide extensions were successfully installed.

2 个答案:

答案 0 :(得分: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上不起作用。

答案 1 :(得分:0)

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