前言:
今天在网上看了很多教程,最后还是在运行pyinstaller命令行时解决
环境配置:python 2.7.9 32位,setuptools 19.2版,WIN 64位
1.安装PyInstaller和pywin32
1.1 Pyinstaller
安装略
可以只是生成单独的可执行程序
且支持的版本也多:2.3到2.7都支持。以及x64也支持
也可以自定义图标
1.2 pywin32
注意选择Python程序对应的版本
https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/
2. 封装成exe
2.1 示范程序用了pandas包
import pandas as pd
import sys,os
import ConfigParser
2.2 pyinstaller使用封装语句
基本语句
pyinstaller -F demo.py
一般来说用这个就可以了
-D, --one-dir打包成一个文件夹,默认
-F, --one-file打包成一个exe文件
-p DIR, --paths DIR添加路径,一般用来添加程序所用到的包的所在位置
-c, --console, --nowindowed提供程序视窗,程序有输入输出的界面,默认
-w, --windowed, --noconsole无视窗,程序后台运行
-i, --icon 添加icon图标
3. 运行时遇到的问题
生成的exe出现ImportError
4. 解决方法
4.1 看CMD提示,在封装时导入缺失的包
pyinstaller -F -w -i xxx.ico yyy.py --hidden-import=pandas._libs.tslibs.timedeltas
4.2 安装对应的hook
太麻烦,此处略
其他注意的东西
文件夹注意...\dist(生成的exe所在文件位置)和...\build(生成的文件夹所在位置)
warnXXX.txt 里面包含错误信息,如
missing module named pyimod03_importers - imported by C:\Python27\lib\site-packages\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\PyInstaller\loader\rthooks\pyi_rth_pkgres.py
missing module named 'multiprocessing.popen_fork' - imported by C:\Python27\lib\site-packages\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\PyInstaller\loader\rthooks\pyi_rth_multiprocessing.py
missing module named 'multiprocessing.popen_spawn_win32' - imported by C:\Python27\lib\site-packages\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\PyInstaller\loader\rthooks\pyi_rth_multiprocessing.py
missing module named 'multiprocessing.spawn' - imported by C:\Python27\lib\site-packages\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\PyInstaller\loader\rthooks\pyi_rth_multiprocessing.py
附录:正确生成exe时控制台信息
c:\Python27>pyinstaller -F Df2Csv.py --hidden-import=pandas._libs.tslibs.timedeltas
116 INFO: PyInstaller: 3.4.dev0-ab8fd9753
116 INFO: Python: 2.7.9
116 INFO: Platform: Windows-8-6.2.9200
116 INFO: wrote c:\Python27\Df2Csv.spec
116 INFO: UPX is not available.
116 INFO: Extending PYTHONPATH with paths
['c:\\Python27', 'c:\\Python27']
116 INFO: checking Analysis
147 INFO: Building because c:\Python27\Df2Csv.py changed
147 INFO: Initializing module dependency graph...
147 INFO: Initializing module graph hooks...
162 INFO: Analyzing hidden import 'pandas._libs.tslibs.timedeltas'
209 INFO: Processing pre-find module path hook distutils
2348 INFO: Processing pre-safe import module hook _xmlplus
5984 INFO: Processing pre-safe import module hook six.moves
8680 INFO: Processing pre-find module path hook site
8680 INFO: site: retargeting to fake-dir 'C:\\Python27\\lib\\site-packages\\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\\PyInstaller\\fake-modules'
8726 INFO: Processing pre-safe import module hook win32com
19948 INFO: running Analysis out00-Analysis.toc
19963 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
required by C:\Python27\python.exe
20095 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest
20111 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_f47e1bd6f6571810.manifest
20111 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_f480bfaef65491a5.manifest
20111 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_f47f259ef656232a.manifest
20252 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.9158_none ...
20267 INFO: Found manifest C:\Windows\WinSxS\Manifests\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_5091b51ebcb97cdc.manifest
20267 INFO: Searching for file msvcr90.dll
20267 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_5091b51ebcb97cdc\msvcr90.dll
20267 INFO: Searching for file msvcp90.dll
20267 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_5091b51ebcb97cdc\msvcp90.dll
20267 INFO: Searching for file msvcm90.dll
20267 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_5091b51ebcb97cdc\msvcm90.dll
20407 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest
20407 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_f47e1bd6f6571810.manifest
20407 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_f480bfaef65491a5.manifest
20407 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9158_none_f47f259ef656232a.manifest
20423 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9158)
20564 INFO: Caching module hooks...
20580 INFO: Analyzing c:\Python27\Df2Csv.py
20595 INFO: Loading module hooks...
20595 INFO: Loading module hook "hook-distutils.py"...
20595 INFO: Loading module hook "hook-sysconfig.py"...
20595 INFO: Loading module hook "hook-xml.py"...
20689 INFO: Loading module hook "hook-httplib.py"...
20689 INFO: Loading module hook "hook-pydoc.py"...
20689 INFO: Excluding import 'Tkinter'
20705 INFO: Removing import of Tkinter from module pydoc
20705 INFO: Loading module hook "hook-_tkinter.py"...
20907 INFO: checking Tree
20955 INFO: checking Tree
20970 INFO: Loading module hook "hook-pkg_resources.py"...
21178 INFO: Loading module hook "hook-pytz.py"...
21281 INFO: Loading module hook "hook-pywintypes.py"...
21515 INFO: Loading module hook "hook-setuptools.py"...
21515 WARNING: Hidden import "setuptools.msvc" not found!
21515 INFO: Loading module hook "hook-encodings.py"...
22281 INFO: Loading module hook "hook-numpy.core.py"...
22344 INFO: Loading module hook "hook-sqlite3.py"...
22438 INFO: Loading module hook "hook-win32com.py"...
22536 INFO: Loading module hook "hook-pythoncom.py"...
22833 INFO: checking Tree
22927 INFO: checking Tree
22974 INFO: checking Tree
23046 INFO: Looking for ctypes DLLs
23126 INFO: Analyzing run-time hooks ...
23126 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
23141 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
23141 INFO: Including run-time hook 'pyi_rth__tkinter.py'
23141 INFO: Including run-time hook 'pyi_rth_pkgres.py'
23173 INFO: Looking for dynamic libraries
23735 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.1_none_7dd1e0ebd6590e0b.manifest
23735 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.4148_none_ecff360cfb2594f3.manifest
23735 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_ed01d9e4fb230e88.manifest
23876 INFO: Searching for assembly x86_Microsoft.VC90.MFC_1fc8b3b9a1e18e3b_9.0.30729.6161_none ...
23876 INFO: Found manifest C:\Windows\WinSxS\Manifests\x86_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_4bf7e3e2bf9ada4c.manifest
23876 INFO: Searching for file mfc90.dll
23876 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_4bf7e3e2bf9ada4c\mfc90.dll
23876 INFO: Searching for file mfc90u.dll
23876 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_4bf7e3e2bf9ada4c\mfc90u.dll
23876 INFO: Searching for file mfcm90.dll
23876 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_4bf7e3e2bf9ada4c\mfcm90.dll
23876 INFO: Searching for file mfcm90u.dll
23876 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_4bf7e3e2bf9ada4c\mfcm90u.dll
24016 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.1_none_7dd1e0ebd6590e0b.manifest
24016 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.4148_none_ecff360cfb2594f3.manifest
24016 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_ed01d9e4fb230e88.manifest
24016 INFO: Adding redirect Microsoft.VC90.MFC version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)
40203 INFO: Looking for eggs
150687 INFO: Using Python library C:\Windows\system32\python27.dll
150687 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'x86', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 9158), publicKeyToken=u'1fc8b3b9a1e18e3b'), BindingRedirect(name=u'Microsoft.VC90.MFC', language=None, arch=u'x86', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 6161), publicKeyToken=u'1fc8b3b9a1e18e3b')]
150718 INFO: Warnings written to c:\Python27\build\Df2Csv\warnDf2Csv.txt
150937 INFO: Graph cross-reference written to c:\Python27\build\Df2Csv\xref-Df2Csv.html
151749 INFO: checking PYZ
151796 INFO: checking PKG
151812 INFO: Building because c:\Python27\Df2Csv.py changed
151812 INFO: Building PKG (CArchive) out00-PKG.pkg
153285 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9158)
162986 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
163538 INFO: Bootloader C:\Python27\lib\site-packages\pyinstaller-3.4.dev0_ab8fd9753-py2.7.egg\PyInstaller\bootloader\Windows-32bit\run.exe
163538 INFO: checking EXE
163554 INFO: Rebuilding out00-EXE.toc because pkg is more recent
163554 INFO: Building EXE from out00-EXE.toc
163570 INFO: Appending archive to EXE c:\Python27\dist\Df2Csv.exe
163601 INFO: Building EXE from out00-EXE.toc completed successfully.