本文介绍了使用pyinstaller打包arm程序的详细过程,可供参考。
因为pyinstaller本身不支持交叉编译,so本文采用的是docker方案。
随便说一句,docker还能加速你的应用部署和环境隔离,简直不要太爽。
环境准备
Linux系统一个,wsl也可
下载好的docker容器一个
一个简单的python Gui程序一套
docker准备
docker pull osrf/ubuntu_arm64
docker images
docker run -it image_id
进入arm64 极简Linux的内部后
这个镜像真的太小了,里面啥都没有,万幸是基于ubuntu24.04的,可以使用apt命令。
apt update
这一步一定要执行,为之后换源做准备,否则会报错
apt install ca-certificates
安装你熟悉的编辑nano/vim....
安装我习惯的编辑器
apt install vim
更换国内源
mv /etc/apt/source.list /etc/apt/soruce.list.bak
vim /etc/apt/source.list.d/ubuntu.sources
添加国内源
Types: deb
URIs: http://ports.ubuntu.com/ubuntu-ports/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
apt update
安装git命令 和 编译工具 (可选)
apt install build-essential git
git 用于远程下载你上传到仓库的项目代码
build-essential中包含了大多数编译用到的命令, 如gcc \ cc等很多工具
如果你不是使用git管理
使用下面命令手动复制项目到容器内
docker cp /path/to/project containerId:/path/to/target执行打包环境准备
apt install python3 python3-pip python3-venv
cd /path/to/target
python3 -m venv venv
source venv/bin/activate
pip install pyinstaller
执行打包
pyinstall -F -wmain.py
-F 打包为一个文件
-w 不要黑框
详细参考官网:https://pyinstaller.org
得到目标文件 dist/target
部署到嵌入式前的准备
查看目标架构是否正确
file dist/target
如果没有file命令 apt install file
如果执行正常,你应该看到aarch64字样而不是x86_64
你可能注意到全程没使用到提权命令`sudo`, 是因为默认就是root用户,拥有最高权限。
领取专属 10元无门槛券
私享最新 技术干货