PyInstaller 将我的 python 脚本构建为可执行文件,但它不起作用

PyInstaller built my python scripts to executable file, but it doesn't work

我正在使用 PyInstaller 创建可执行文件。

我的规格文件如下:

# -*- mode: python -*-
import sys

a = Analysis(['maingui.py'],
         hiddenimports=[],
         hookspath=None)
for d in a.datas:
    if 'pyconfig' in d[0]: 
    a.datas.remove(d)
    break
pyz = PYZ(a.pure)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas + [('scheme_1e1s.png', '.\scheme_1e1s.png', 'DATA')],
      [('scheme_2e1s.png', '.\scheme_2e1s.png', 'DATA')],
      [('scheme_2e2s.png', '.\scheme_2e2s.png', 'DATA')],
      [('scheme_2ew1s.png', '.\scheme_2ew1s.png', 'DATA')],
      [('scheme_3e1s.png', '.\scheme_3e1s.png', 'DATA')],
      [('scheme_3e1s_inter.png', '.\scheme_3e1s_inter.png', 'DATA')],
      [('scheme_3e1s_series.png', '.\scheme_3e1s_series.png', 'DATA')],
      [('scheme_3e2s.png', '.\scheme_3e2s.png', 'DATA')],
      [('scheme_3e2s_inter.png', '.\scheme_3e2s_inter.png', 'DATA')],
      [('scheme_3e2s_series.png', '.\scheme_3e2s_series.png', 'DATA')],
      [('chinese.qm', '.\chinese.qm', 'DATA')],
      [('spanish.qm', '.\spanish.qm', 'DATA')],
      name=os.path.join('dist', 'maingui.exe'),
      debug=False,
      strip=None,
      upx=True,
      console=True)

app = BUNDLE(exe,
      name='maingui.exe.app',
      icon=None)

当我运行 PyInstaller时,它会生成exe文件并成功分发。

c:\bin\chem>pyinstaller --clean maingui.spec
39 INFO: Testing for ability to set icons, version resources...
72 INFO: ... resource update available
76 INFO: UPX is not available.
101 INFO: Processing hook hook-os
222 INFO: Processing hook hook-time
227 INFO: Processing hook hook-cPickle
300 INFO: Processing hook hook-_sre
434 INFO: Processing hook hook-cStringIO
539 INFO: Processing hook hook-encodings
552 INFO: Processing hook hook-codecs
993 INFO: Removing temporary files and cleaning cache in C:\Users\super2lao\AppData\Roaming\pyinstal
ler
1161 INFO: Extending PYTHONPATH with c:\bin\chem
1161 INFO: checking Analysis
1161 INFO: building Analysis because out00-Analysis.toc non existent
1161 INFO: running Analysis out00-Analysis.toc
1162 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
1296 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none ...
1296 INFO: Found manifest C:\Windows\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.
21022.8_none_750b37ff97f4f68b.manifest
1297 INFO: Searching for file msvcr90.dll
1299 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_7
50b37ff97f4f68b\msvcr90.dll
1299 INFO: Searching for file msvcp90.dll
1299 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_7
50b37ff97f4f68b\msvcp90.dll
1299 INFO: Searching for file msvcm90.dll
1299 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_7
50b37ff97f4f68b\msvcm90.dll
1401 INFO: Analyzing c:\bin\Python27\lib\site-packages\PyInstaller\loader\_pyi_bootstrap.py
1412 INFO: Processing hook hook-os
1428 INFO: Processing hook hook-site
1447 INFO: Processing hook hook-encodings
1570 INFO: Processing hook hook-time
1573 INFO: Processing hook hook-cPickle
1651 INFO: Processing hook hook-_sre
1783 INFO: Processing hook hook-cStringIO
1907 INFO: Processing hook hook-codecs
2370 INFO: Processing hook hook-pydoc
2509 INFO: Processing hook hook-email
2573 INFO: Processing hook hook-httplib
2616 INFO: Processing hook hook-email.message
2684 INFO: Analyzing c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_importers.py
2736 INFO: Analyzing c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_archive.py
2783 INFO: Analyzing c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_carchive.py
2832 INFO: Analyzing c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_os_path.py
2839 INFO: Analyzing maingui.py
2953 INFO: Processing hook hook-PySide
3226 INFO: Processing hook hook-matplotlib
12008 INFO: Processing hook hook-matplotlib.backends
13428 INFO: Processing hook hook-parser
13579 INFO: Processing hook hook-distutils
14027 INFO: Processing hook hook-setuptools
14319 INFO: Processing hook hook-sysconfig
14351 INFO: Processing hook hook-xml
14416 INFO: Processing hook hook-xml.sax
14450 INFO: Processing hook hook-pyexpat
14529 INFO: Processing hook hook-win32com
14537 INFO: Processing hook hook-win32com.client
14553 INFO: Processing hook hook-pythoncom
14620 INFO: Processing hook hook-pywintypes
14774 INFO: Processing hook hook-win32ui
15550 INFO: Processing hook hook-scipy.sparse.csgraph
16533 INFO: Processing hook hook-PyQt4
16536 INFO: Processing hook hook-PyQt4.QtGui
17297 INFO: Processing hook hook-PyQt4.QtCore
18200 INFO: Hidden import 'codecs' has been found otherwise
18201 INFO: Hidden import 'encodings' has been found otherwise
18201 INFO: Looking for run-time hooks
18203 INFO: Analyzing rthook c:\bin\Python27\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_qt
4plugins.py
18247 INFO: Analyzing rthook c:\bin\Python27\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_mp
lconfig.py
18304 INFO: Analyzing rthook c:\bin\Python27\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_mp
ldata.py
18347 INFO: Analyzing rthook c:\bin\Python27\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pk
gres.py
18494 INFO: Analyzing rthook c:\bin\Python27\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_wi
n32comgenpy.py
28392 INFO: Adding Microsoft.VC90.MFC to dependent assemblies of final executable
28521 INFO: Searching for assembly amd64_Microsoft.VC90.MFC_1fc8b3b9a1e18e3b_9.0.21022.8_none ...
28523 INFO: Found manifest C:\Windows\WinSxS\Manifests\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0
.21022.8_none_706fccb39ad7e580.manifest
28524 INFO: Searching for file mfc90.dll
28526 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.21022.8_none_
706fccb39ad7e580\mfc90.dll
28526 INFO: Searching for file mfc90u.dll
28526 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.21022.8_none_
706fccb39ad7e580\mfc90u.dll
28527 INFO: Searching for file mfcm90.dll
28527 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.21022.8_none_
706fccb39ad7e580\mfcm90.dll
28534 INFO: Searching for file mfcm90u.dll
28536 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.21022.8_none_
706fccb39ad7e580\mfcm90u.dll
32259 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
32444 INFO: Using Python library C:\Windows\system32\python27.dll
32641 INFO: Warnings written to c:\bin\chem\build\maingui\warnmaingui.txt
32684 INFO: checking PYZ
32685 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
32686 INFO: building PYZ (ZlibArchive) out00-PYZ.toc
38406 INFO: checking PKG
38407 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
38407 INFO: building PKG (CArchive) out00-PKG.pkg
80056 INFO: checking EXE
80057 INFO: building because out00-EXE.toc missing or bad
80059 INFO: building EXE from out00-EXE.toc
80093 INFO: Appending archive to EXE c:\bin\chem\dist\maingui.exe

c:\bin\chem>

顺便说一句,我得到以下错误,这是我的一个很大的困难。

c:\bin\chem>dist\maingui.exe
Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_mo
dule
    exec(bytecode, module.__dict__)
  File "c:\bin\chem\build\maingui\out00-PYZ.pyz\PySide", line 41, in <module>
  File "c:\bin\chem\build\maingui\out00-PYZ.pyz\PySide", line 11, in _setupQtDirectories
  File "c:\bin\chem\build\maingui\out00-PYZ.pyz\PySide._utils", line 93, in get_pyside_dir
  File "c:\bin\Python27\lib\site-packages\PyInstaller\loader\pyi_importers.py", line 409, in load_mo
dule
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
RuntimeError: the sip module has already registered a module called PyQt4.QtCore

c:\bin\chem>

怎么了?

我解决了这个问题。规格文件中有错误。 pyinstaller maingui.py 此命令生成一个规范文件。我用了这个,得到了一个 运行 exe 文件。

很高兴解决这些问题。

此致。