我在Windows 7(64位)上玩过PyInstaller。我目前正在使用PyInstaller 3.1.1和Python 2.7.6。我创建了一个创建简单PySide GUI的应用程序,并使用此命令生成带有可执行文件的dist文件夹:
> C:\Python27\Scripts\pyinstaller.exe .\HelloWidget.spec
这是spec文件的样子:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['HelloWidget.py'],
pathex=['C:\\Users\\spearsc\\Documents\\python_projects\\HelloWorldGui'],
binaries=None,
datas=[('.\\mainwindow.ui', '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='HelloWidget',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='HelloWidget')
我想使用spec文件,因此.ui文件将与其他所有内容捆绑在一起。但是,当我运行可执行文件时,我收到此错误:
> .\HelloWidget.exe
Traceback (most recent call last):
File "<string>", line 41, in <module>
File "c:\users\spearsc\appdata\local\temp\pip-build-djws3k\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line
389, in load_module
File "PySide-1.2.2\PySide\__init__.py", line 55, in <module>
File "PySide-1.2.2\PySide\__init__.py", line 11, in _setupQtDirectories
File "PySide-1.2.2\PySide\_utils.py", line 87, in get_pyside_dir
File "PySide-1.2.2\PySide\_utils.py", line 83, in _get_win32_case_sensitive_name
File "PySide-1.2.2\PySide\_utils.py", line 58, in _get_win32_short_name
WindowsError: [Error 2] The system cannot find the file specified.
pyi_rth_qt4plugins returned -1
令我困惑的是,当我查看dist文件夹时,我看到编译的PySide和Qt文件:
Directory: C:\Users\spearsc\Documents\python_projects\HelloWorldGui\dist\HelloWidget
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/23/2016 4:45 PM 80896 bz2.pyd
-a--- 5/24/2016 5:20 PM 795278 HelloWidget.exe
-a--- 5/24/2016 5:20 PM 1019 HelloWidget.exe.manifest
-a--- 5/24/2016 4:42 PM 4102 mainwindow.ui
-a--- 5/24/2016 5:20 PM 1052 Microsoft.VC90.CRT.manifest
-a--- 5/23/2016 4:45 PM 245760 msvcm90.dll
-a--- 5/23/2016 4:45 PM 853328 msvcp90.dll
-a--- 5/23/2016 4:45 PM 641360 msvcr90.dll
-a--- 5/24/2016 4:43 PM 248320 pyside-python2.7.dll
-a--- 5/24/2016 4:43 PM 3063808 PySide.QtCore.pyd
-a--- 5/24/2016 4:43 PM 12750848 PySide.QtGui.pyd
-a--- 5/24/2016 4:43 PM 1095168 PySide.QtNetwork.pyd
-a--- 5/24/2016 4:43 PM 1052672 PySide.QtUiTools.pyd
-a--- 5/23/2016 4:45 PM 3004928 python27.dll
-a--- 5/24/2016 4:43 PM 3469824 QtCore4.dll
-a--- 5/24/2016 4:43 PM 11679744 QtGui4.dll
-a--- 5/24/2016 4:43 PM 1473536 QtNetwork4.dll
-a--- 5/23/2016 4:45 PM 10752 select.pyd
-a--- 5/24/2016 4:43 PM 330752 shiboken-python2.7.dll
-a--- 5/23/2016 4:45 PM 689664 unicodedata.pyd
-a--- 5/24/2016 4:43 PM 111616 _ctypes.pyd
-a--- 5/23/2016 4:45 PM 474624 _hashlib.pyd
有什么想法?我可以不相信我在dist文件夹中看到的内容吗?
16年5月25日:
嗯,这很有意思。我在_get_win32_short_name函数中放了一个print语句。
def _get_win32_short_name(s):
""" Returns short name """
print s
buf_size = MAX_PATH
for i in range(2):
buf = create_unicode_buffer(u('\0') * (buf_size + 1))
r = GetShortPathNameW(u(s), buf, buf_size)
if r == 0:
raise WinError()
if r < buf_size:
if PY_2:
return buf.value.encode(sys.getfilesystemencoding())
return buf.value
buf_size = r
raise WinError()
这是我在构建新的可执行文件后得到的结果。
> .\HelloWidget.exe
C:\Users\spearsc\DOCUME~1\PYTHON~1\HELLOW~1\dist\HELLOW~1\PySide
Traceback (most recent call last):
File "<string>", line 41, in <module>
File "c:\users\spearsc\appdata\local\temp\pip-build-djws3k\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line
389, in load_module
File "PySide-1.2.2\PySide\__init__.py", line 55, in <module>
File "PySide-1.2.2\PySide\__init__.py", line 11, in _setupQtDirectories
File "PySide-1.2.2\PySide\_utils.py", line 88, in get_pyside_dir
File "PySide-1.2.2\PySide\_utils.py", line 84, in _get_win32_case_sensitive_name
File "PySide-1.2.2\PySide\_utils.py", line 59, in _get_win32_short_name
WindowsError: [Error 2] The system cannot find the file specified.
pyi_rth_qt4plugins returned -1
该目录不存在。关于如何解决这个问题的任何想法?
答案 0 :(得分:0)
诀窍是在dist文件夹中添加一个空的PySide文件夹。
Directory: C:\Users\spearsc\Documents\python_projects\HelloWorldGui\dist\HelloWidget
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/25/2016 2:46 PM PySide
-a--- 5/23/2016 4:45 PM 80896 bz2.pyd
-a--- 5/25/2016 2:46 PM 795278 HelloWidget.exe
-a--- 5/25/2016 2:46 PM 1019 HelloWidget.exe.manifest
-a--- 5/24/2016 4:42 PM 4102 mainwindow.ui
-a--- 5/25/2016 2:46 PM 1052 Microsoft.VC90.CRT.manifest
-a--- 5/23/2016 4:45 PM 245760 msvcm90.dll
-a--- 5/23/2016 4:45 PM 853328 msvcp90.dll
-a--- 5/23/2016 4:45 PM 641360 msvcr90.dll
-a--- 5/24/2016 4:43 PM 248320 pyside-python2.7.dll
-a--- 5/24/2016 4:43 PM 3063808 PySide.QtCore.pyd
-a--- 5/24/2016 4:43 PM 12750848 PySide.QtGui.pyd
-a--- 5/24/2016 4:43 PM 1095168 PySide.QtNetwork.pyd
-a--- 5/24/2016 4:43 PM 1052672 PySide.QtUiTools.pyd
-a--- 5/25/2016 2:46 PM 542208 PySide.QtXml.pyd
-a--- 5/23/2016 4:45 PM 3004928 python27.dll
-a--- 5/24/2016 4:43 PM 3469824 QtCore4.dll
-a--- 5/24/2016 4:43 PM 11679744 QtGui4.dll
-a--- 5/24/2016 4:43 PM 1473536 QtNetwork4.dll
-a--- 5/25/2016 2:46 PM 506368 QtXml4.dll
-a--- 5/23/2016 4:45 PM 10752 select.pyd
-a--- 5/24/2016 4:43 PM 330752 shiboken-python2.7.dll
-a--- 5/23/2016 4:45 PM 689664 unicodedata.pyd
-a--- 5/24/2016 4:43 PM 111616 _ctypes.pyd
-a--- 5/23/2016 4:45 PM 474624 _hashlib.pyd
在我这样做之后,我运行了可执行文件,并为PySide.QtXml导致了导入错误,因此我生成了一个新的spec文件并对其进行了修改。
# -*- mode: python -*-
block_cipher = None
a = Analysis(['HelloWidget.py'],
pathex=['C:\\Users\\spearsc\\Documents\\python_projects\\HelloWorldGui'],
binaries=None,
datas=[('.\mainwindow.ui', '.')],
hiddenimports=['PySide.QtXml'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='HelloWidget',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='HelloWidget')
然后我使用spec文件再次创建可执行文件。我将空的PySide添加到dist文件夹中,它有效!