使用pyinstaller将python程序打包成exe/苹果软件的方法与异常解决
Song • 10214 次浏览 • 0 个回复 • 2020年12月26日

一、安装pyinstaller
通过清华镜像安装比较快
# 二者选其一执行,第二条快
pip3 installer pyinstaller
# 清华镜像安装
# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
二、安装以后 运行pyinstaller打包命令
pyinstaller test.py --noconsole -i logo.ico
pyinstaller test.py --noconsole -i logo.ico
其中test.py
是主程序文件logo.ico
是图标
好了 这里小编遇到一个问题 提示无法找到命令,对比windos而言 这里肯定也是需要配置环境变量 ,所以我们首先需要找到pyinstaller的可执行文件(windosws的在python安装目录下 一个script的文件夹下面 将这个目录配置成环境变量即可)
三、使用方法
- -h,--help 查看该模块的帮助信息
- -F,-onefile 产生单个的可执行文件
- -D,--onedir 产生一个目录(包含多个文件)作为可执行程序
- -a,--ascii 不包含 Unicode 字符集支持
- -d,--debug 产生 debug 版本的可执行文件
- -w,--windowed,--noconsolc 指定程序运行时不显示命令行窗口(仅对 Windows 有效)
- -c,--nowindowed,--console 指定使用命令行窗口运行程序(仅对 Windows 有效)
- -o DIR,--out=DIR 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
- -p DIR,--path=DIR 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
- -n NAME,--name=NAME 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字
解决异常
1、安装后找不到pyinstaller
执行pyinstaller -v
提示pyinstaller: command not found
通过find / -name "pyinstaller"
查找到pyinstaller
find: /System/Volumes/Data/Users/Guest/Movies: Permission denied
find: /System/Volumes/Data/Users/Guest/Documents: Permission denied
find: /System/Volumes/Data/Users/Guest/Downloads: Permission denied
/System/Volumes/Data/Users/**/Library/Python/3.7/bin/pyinstaller
find: /System/DriverKit: No such file or directory
find: /.fseventsd: Permission denied
find: /private/etc/cups/certs: Permission denied
find: /private/var/networkd/Library: Permission denied
find: /private/var/install: Permission denied
通过vim ~/.bash_profile
将配置到环境变量中
export PATH=/System/Volumes/Data/Users/**/Library/Python/3.7/bin:$PATH
然后source ~/.bash_profile
出重新编译配置生效。
2、Mac Osx/Windos 使用pyinstalle 打包后图标不生效
我尝试了很久才发现,Mac下图标文件为icns
格式Win为ico
# Mac 使用pyinstalle打包配置图标
pyinstaller test.py --noconsole --icon=logo.icns
# Windos 使用pyinstalle打包配置图标
pyinstaller test.py --noconsole --icon=logo.ico
打包好后把你的应用拖到电脑的应用程序
下就可以像Mac版的QQ
一样点击使用了。Win同理。
3. 我可以用 pyinstaller 在Linux下运行时打包Windows二进制文件吗?
不,这是不支持的。请用Wine来做这件事,用Wine很好。你可能还想看看邮件列表中的这个线程。在版本1.4中,我们内置了一些对此的支持,但它只工作了一半。它需要在另一个分区上安装一些Windows系统,并且只适用于纯Python程序。只要你想要一个像样的GUI(gtk,qt,wx),你就需要安装Windows库了。所以用酒就容易多了。
4. 在OSX下运行时,我可以用 pyinstaller 打包Windows二进制文件吗?
不,这是不支持的。请用Wine。
参考:mac系统通过pyinstaller打包桌面应用程序 github地址: pyinstaller/pyinstaller
原创文章,转载请注明 :使用pyinstaller将python程序打包成exe/苹果软件的方法与异常解决 - pytorch中文网
原文出处: https://ptorch.com/news/266.html
问题交流群 :168117787
- 没有评论