目录
一、为什么要打包
二、打包后文件大的问题
三、外部传参
3.1、sys库
3.2、argparse库
四、pyinstaller打包
4.1、安装包
4.2、打包命令
4.3、新环境打包
五、运行exe程序
5.1、sys接收外部传参
5.2、argparse接收外部传参
就比如你在具有Python环境的A电脑上开发了一个程序,现在A电脑坏掉了,你只能在B电脑上去执行你开发的程序。但是呢B电脑又没有Python环境,短时间又不能解决环境问题。这时候就需要用到Python的打包了。
打完包的exe文件拿到任何一台电脑上都可以跑,这就方便了许多。本文介绍如何打包以及打包后的exe文件支持外部传参。
这里我遇到的一个问题就是我直接使用Python的打包造成打包出来的文件过大,会不方便移植。这可能就是打包程序会默认把lib\site-packages下面一些无关紧要的Python包打进去,但实际上程序里都没用到。
最简单的解决方法就是创建一个虚拟环境,然后把你要打包的程序所需要的包下载一遍。再使用新环境对你的程序打包。这样就能减少很多空间。下面是我新建环境打的包和原环境打的包,显而易见,区别还是蛮大的。
关于Python虚拟环境可以参考:windows系统下创建Python虚拟环境
要解决打包后的exe支持接收外部传参的功能。这里就用到了sys包或者argparse包,两者都能达到接收外部参数的目的。下面介绍两种方式传参。
import sys
# 解析参数
in_filepath = sys.argv[1]
out_style = sys.argv[2]
out_filepath = sys.argv[3]
print("--input_filepath:",in_filepath)
print("--output_file_style",out_style)
print("--output_filepath",out_filepath)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input_filepath', type=str , help = 'Select the PDF you want to analyze')
parser.add_argument('--output_file_style', type=str, help='output_style:csv,xlsx') # 1 for dn and jpeg car
parser.add_argument('--output_filepath', type=str, help='Select the output path')
args = parser.parse_args()
# 解析参数
in_filepath = args.input_filepath
out_style = args.output_file_style
out_filepath = args.output_filepath
print("--input_filepath:",in_filepath)
print("--output_file_style",out_style)
print("--output_filepath",out_filepath)
其实市面上Python打包库很多,列如py2exe,pyinstaller,nuitka 等。pyinstaller简单实用,打包命令简介易懂。
除了pyinstaller要安装外,还需要检查下程序所用到的包。然后一起安装到新环境中,最后再使用打包命令。
pip install pyinstaller
如果想要给你打包好的程序加上一个图标,加上 -i 这个参数,后面跟ico图片的路径即可。
pyinstaller -F -i your_img.ico your.py
激活新环境(activate your_new_env),然后输入打包命令即可。
(pdf2table_exe) C:\Users\dell>pyinstaller -F D:\Gitlab\my_world\recognize_img\pdf2form\pdf2table.py
130 INFO: PyInstaller: 4.10
130 INFO: Python: 3.6.3 (conda)
131 INFO: Platform: Windows-10-10.0.19041-SP0
131 INFO: wrote C:\Users\dell\pdf2table.spec
133 INFO: UPX is not available.
135 INFO: Extending PYTHONPATH with paths
['D:\\Gitlab\\my_world\\recognize_img\\pdf2form']
308 INFO: checking Analysis
308 INFO: Building Analysis because Analysis-00.toc is non existent
308 INFO: Initializing module dependency graph...
311 INFO: Caching module graph hooks...
319 INFO: Analyzing base_library.zip ...
2198 INFO: Caching module dependency graph...
2305 INFO: running Analysis Analysis-00.toc
2319 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\Users\dell\Anaconda3\envs\pdf2table_exe\python.exe
2458 INFO: Analyzing D:\Gitlab\my_world\recognize_img\pdf2form\pdf2table.py
18069 INFO: Looking for dynamic libraries
18730 INFO: Looking for eggs
18730 INFO: Using Python library C:\Users\dell\Anaconda3\envs\pdf2table_exe\python36.dll
18730 INFO: Found binding redirects:
[]
18745 INFO: Warnings written to C:\Users\dell\build\pdf2table\warn-pdf2table.txt
18907 INFO: Graph cross-reference written to C:\Users\dell\build\pdf2table\xref-pdf2table.html
18958 INFO: checking PYZ
18958 INFO: Building PYZ because PYZ-00.toc is non existent
18960 INFO: Building PYZ (ZlibArchive) C:\Users\dell\build\pdf2table\PYZ-00.pyz
20398 INFO: Building PYZ (ZlibArchive) C:\Users\dell\build\pdf2table\PYZ-00.pyz completed successfully.
20430 INFO: checking PKG
20430 INFO: Building PKG because PKG-00.toc is non existent
20431 INFO: Building PKG (CArchive) pdf2table.pkg
35758 INFO: Building PKG (CArchive) pdf2table.pkg completed successfully.
35780 INFO: Bootloader C:\Users\dell\Anaconda3\envs\pdf2table_exe\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
35781 INFO: checking EXE
35781 INFO: Building EXE because EXE-00.toc is non existent
35782 INFO: Building EXE from EXE-00.toc
35782 INFO: Copying bootloader EXE to C:\Users\dell\dist\pdf2table.exe.notanexecutable
35789 INFO: Copying icon to EXE
35789 INFO: Copying icons from ['C:\\Users\\dell\\Anaconda3\\envs\\pdf2table_exe\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
35790 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
35791 INFO: Writing RT_ICON 1 resource with 3752 bytes
35791 INFO: Writing RT_ICON 2 resource with 2216 bytes
35791 INFO: Writing RT_ICON 3 resource with 1384 bytes
35791 INFO: Writing RT_ICON 4 resource with 37019 bytes
35791 INFO: Writing RT_ICON 5 resource with 9640 bytes
35791 INFO: Writing RT_ICON 6 resource with 4264 bytes
35791 INFO: Writing RT_ICON 7 resource with 1128 bytes
35797 INFO: Copying 0 resources to EXE
35797 INFO: Emedding manifest in EXE
35798 INFO: Updating manifest in C:\Users\dell\dist\pdf2table.exe.notanexecutable
35798 INFO: Updating resource type 24 name 1 language 0
35802 INFO: Appending PKG archive to EXE
48797 INFO: Building EXE from EXE-00.toc completed successfully.
看到打包成功后,在打包的目录下找到以py文件的名称命名的exe文件。
命令:名称.exe 参数1 参数2 参数n
命令:pdf2table.exe --input_filepath 参数1 --output_file_style 参数2 --output_filepath 参数3