Python的带pandas包的程序封装成exe 2018-01-11


前言:

今天在网上看了很多教程,最后还是在运行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 <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns>添加icon图标

3. 运行时遇到的问题

生成的exe出现ImportError

image.png

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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343