2012-07-04 28 views
5

Sử dụng mới nhất pyinstaller trên Windows 7 để thực hiện một exe độc ​​lập (-F), khi chạy exe:PyInstaller: ImportError: không thể nhập tên QtGui

ImportError: cannot import name QtGui

Trong thư mục móc PyInstaller có xử lý đặc biệt cho PyQt4 dù không phải PySide.

Hy vọng giải pháp cho việc này hoặc điều gì đó cần thử.

Môi trường
Windows 7 64-bit
Python 2.7 32-bit
PYTHONHOME = c: \ python27
PYTHONPATH = c: \ python27 \ lib
PYTHONLIB = c: \ python27 \ libs \ python27.lib; c: \ python27 \ lib \ site-packages

bước
1. Thêm PySide từ http://releases.qt-project.org/pyside/1.1.1/PySide-1.1.1qt474.win32-py2.7.exe
2. Giải nén https://github.com/pyinstaller/pyinstaller/zipball/develop vào c: \ pyinstaller1.5.1
3. Chạy các lệnh dưới đây chống lại một tập tin py chứa đơn giản: [. ... hoặc QtCore hay hay]

from PySide import QtGui 

Run

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 

Note

 
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. 

Trả lời

1

Cách giải quyết. Điều này làm việc:

# 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 nên làm một công việc tốt của vị phụ thuộc, và thường làm. Tuy nhiên, tất cả nhiều nỗ lực để sử dụng pathex hoặc hiddenimports của nó trong .spec đều không thành công. Sửa đổi các biến môi trường của tôi cũng không thành công. Trích xuất các tệp mô-đun khác nhau theo cách thủ công từ từ .egg đôi khi hoạt động.

Tuy nhiên đối với nhập PySide, giới hạn tạm thời sys.path ở trên là giải pháp thay thế hoạt động.

Cập nhật: Rất tiếc, exe chỉ hoạt động trên máy có cài đặt Python/Pyside, không hoạt động trên XP mà không có Python.

Các vấn đề liên quan