本文主要是介绍ubuntu20.04 | Python单文件打包二进制的方式——Pyinstaller,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python比较麻烦的一点是环境依赖很重,如果直接用脚本在不同环境使用,必定会导致结果不一致的情况。
所以,将python打包成二进制进行发布,就避免了这个问题。
pyinstaller
用于将Python文件和依赖包打包成为Linux可执行程序,方便在不同机器调用。
跨平台,支持win,nux,mac。
打包环境
ubuntu20.04
python3
1.安装步骤
- 1>安装pip/pip3
sudo apt-get install python3-pip
- 2>更换pip3下载源
sudo sumkdir -p /root/.pipcd /root/.pipvim pip.conf
3> 写入pip.conf
[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simple[install]trusted-host=pypi.tuna.tsinghua.edu.cn
4> 安装pyinstaller
pip3 install pyinstaller
2.使用方式
常用如下参数:
What to generate:-D, --onedir Create a one-folder bundle containing an executable (default)-F, --onefile Create a one-file bundled executable.--specpath DIR Folder to store the generated spec file (default: current directory)-n NAME, --name NAME Name to assign to the bundled app and spec file (default: first script's basename)
一般使用方式
回到要打包的文件所在目录
pyinstaller -F 要打包的.py文件
执行完毕后,源文件所在目录将生成 dist 和 build 两个文件夹。
- build 目录是 pyinstaller 存储临时文件的目录,可以安全删除
- dist 目录存储二进制相关内容。目录中其他文件是可执行文件 dpython.exe 的动态链接库。
生成目录如下:
打包二进制目录如图所示
注意:
- 再次生成时,需要删除上次生成的二进制文件
- 注意打包机器和二进制使用的系统保持一致
- python打包也可以使用docker,后续有空再整理一波
更多使用方式:pyinstaller -h
这篇关于ubuntu20.04 | Python单文件打包二进制的方式——Pyinstaller的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!