2012-07-04 208 views
5

使用在Windows 7最新pyinstaller做一個獨立的EXE(-F),運行exe文件時:Pyinstaller:導入錯誤:無法導入名稱QtGui

ImportError: cannot import name QtGui

在pyinstaller鉤目錄中有特殊處理PyQt4雖然不是PySide。

希望爲此解決方法或嘗試。

環境
視窗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.exePySide 2.解壓https://github.com/pyinstaller/pyinstaller/zipball/develop到c:\ pyinstaller1.5.1
3.運行下面的命令針對.py文件含有簡單:[...或QtCore或或]

from PySide import QtGui 

運行

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. 

回答

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或hiddenimport的許多嘗試都失敗了。 修改我的環境變量也失敗了。 從.egg手動提取各種模塊文件有時可用。

但是,對於PySide導入,上面的sys.path臨時限制是有效的解決方法。

更新:不幸的是,該exe只適用於安裝了Python/Pyside的機器,在沒有Python的情況下XP無法正常工作。