python34 pandas_【整理】PyInstaller针对包含Python3.4, PyQt, Pandas, Multiprocess的程序打包 | 勤奋的小青蛙...

最近使用PyInstaller对我的Python程序打包。

环境:Python 3.4.3 32 bit+PyQt 4.8.7 for py3.4 32 bit + Pandas + Multiprocess

打包中有几个坑,一定要越过去,不然会死的很惨。

1:支持multiprocess

当你使用多进程的模块时,一定要在 __main__方法下面紧跟着加上如下代码:

multiprocessing.freeze_support()

这种方式只针对打包中的"--onedir"模式,如果是"--onefile",那就要采用另外方法了,参考:Recipe Multiprocessing

2:支持Pandas库

Pandas是用C写的一套python库,所以,在支持C库打包的时候,必须采用pyinstaller的spec方式打包,把打包的命令各种写到 spec 文件里。为了支持pandas,必须这么干:

在spec文件中,在

a = Analysis(...)这下面,紧接着加入下面代码:

def get_pandas_path():

import pandas

pandas_path = pandas.__path__[0]

return pandas_path

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])

a.datas += dict_tree

a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

这样子就支持pandas了。

3:打包好的程序,运行始终报错:“ImportError: DLL load failed:找不到指定模块”

首先在代码中解决该问题,我们追加系统path到我们当前的目录,这段代码建议放在main方法开头,不过如果支持multiprocess的话,那就放在支持multiprocess代码下:

frozen = 'not'

if getattr(sys, 'frozen', False):

# we are running in a bundle

frozen = 'ever so'

bundle_dir = sys._MEIPASS

else:

# we are running in a normal Python environment

bundle_dir = os.path.dirname(os.path.abspath(__file__))

win32api.SetDllDirectory(bundle_dir)

sys.path.append(bundle_dir)

再次打包,如果依然报错,那么,建议安装

只要把微软常用运行库安装好了,缺失DLL的问题就迎刃而解了。

最后,放出我的打包spec文件

ClientMain.spec:

# -*- mode: python -*-

block_cipher = None

def get_pandas_path():

import pandas

pandas_path = pandas.__path__[0]

return pandas_path

added_files = [('D:\\Python34\\DLLs\\python3.dll', '.'),('*.dll', '.'),('img', 'img'),('image', 'image'),('config', 'config')]

a = Analysis(['ClientMain.py'],

pathex=['.', 'D:\\CTP\\PyCTP\\PyCTP_Client\\PyCTP_ClientCore'],

binaries=[('PyCTP.pyd',''),

('thostmduserapi.dll',''),

('thosttraderapi.dll','')],

datas=added_files,

hiddenimports=[],

hookspath=[],

runtime_hooks=[],

excludes=[],

win_no_prefer_redirects=False,

win_private_assemblies=False,

cipher=block_cipher)

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])

a.datas += dict_tree

a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

pyz = PYZ(a.pure, a.zipped_data,

cipher=block_cipher)

exe = EXE(pyz,

a.scripts,

exclude_binaries=True,

name='ClientMain',

debug=False,

strip=False,

upx=False,

console=True , icon='img\\rocket.ico')

coll = COLLECT(exe,

a.binaries,

a.zipfiles,

a.datas,

strip=False,

upx=False,

name='ClientMain')

打包执行命令:

pyinstaller --onedir --noupx ClientMain.spec

文章的脚注信息由WordPress的wp-posturl插件自动生成

|2|left

打赏

微信扫一扫,打赏作者吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值