赞
踩
pip install pyinstaller
ps:遇到问题可以去 whl 文件仓库 下载
| 可选参数 | 格式举例 | 功能说明 |
|---|---|---|
-F | pyinstaller -F demo.py | 只在dist中生产一个demo.exe文件。 |
-D | pyinstaller -D demo.py | 默认选项,除了demo.exe外,还会在在dist中生成很多依赖文件,推荐使用。 |
-c | pyinstaller -c demo.py | 默认选项,只对windows有效,使用控制台,就像编译运行C程序后的黑色弹窗。 |
-w | pyinstaller -w demo.py | 只对windows有效,不使用控制台。 |
-p | pyinstaller -p E:\python\Lib\site-packages demo.py | 设置导入路径,一般用不到。 |
-i | pyinstaller -i D:\file.icon demo.py | 将file.icon设置为exe文件的图标,推荐一个icon网站:icon |
ps:1)上面的可选参数可以组合使用,比如pyinstaller -F -i D:\file.icon demo.py。
2)能够from xxx import yyy就尽量不要import xxx,这样可以减少打包后的体积。
- pyinstaller [主文件] -p [其他文件1] -p [其他文件2]
- --hidden-import [自建模块1]
- --hidden-import [自建模块2]
- # 以上为一整条命令
- pyinstaller demo1.py demo2.py
- #----------------------------------
- pyinstaller main.py -p mysql.py -p other.py --hidden-import mysql --hidden-import other
-
-
- #-------------------
- #在当前的目录下,将会生成两个文件夹:build和dist。dist里面就是所有可执行文件,点击demo.exe就能运行了。
- #-------------------
ps:注意路径中不要有中文
一般而言,pyinstaller的基本用法已经够用了,但是有特殊需求,比如打包图片资源文件时,就必须用到它的高阶功法了。
首先得了解spec文件,简而言之,spec文件就是一份告诉pyinstaller如何打包的配置文件。
可以通过pyi-makespec demo.py来生成demo.spec文件。其内容如下:
- # -*- mode: python -*-
-
- block_cipher = None
-
- resources = (("inspurer.db", "."), ("dlib_face_recognition_resnet_model_v1.dat", "."),
- ("shape_predictor_68_face_landmarks.dat", "."), ("close_logcat.png", ".")
- , ("open_logcat.png", "."), ("finish_register.png", "."), ("new_register.png", ".")
- , ("start_punchcard.png", "."), ("end_puncard.png", "."), ("index.png", "."))
-
- a = Analysis(['workAttendanceSystem.py'],
- pathex=['C:\\Users\\lenovo\\Desktop\\test\\python'],
- binaries=[],
- datas=resources,
- hiddenimports=[],
- hookspath=[],
- runtime_hooks=[],
- excludes=[],
- win_no_prefer_redirects=False,
- win_private_assemblies=False,
- cipher=block_cipher,
- noarchive=False)
- pyz = PYZ(a.pure, a.zipped_data,
- cipher=block_cipher)
- exe = EXE(pyz,
- a.scripts,
- [],
- exclude_binaries=True,
- name='workAttendanceSystem',
- debug=False,
- bootloader_ignore_signals=False,
- strip=False,
- upx=True,
- console=True )
- coll = COLLECT(exe,
- a.binaries,
- a.zipfiles,
- a.datas,
- strip=False,
- upx=True,
- name='workAttendanceSystem')
对于上面这个文件,需要注意两点:
除了resources配置是我添加修改之外,其余全是自动生成,这个配置是用来添加资源文件的。
pathex是工程的根目录。
生成并配置好spec文件后,我们可以通过pyinstaller demo.spec来执行打包任务。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。