Ubuntu下,用Pyinstaller打包桌面系统时,你踩坑了没?

前几天,在Ubuntu下,用Pyinstaller打包wxPython开发的桌面系统时,遇到不少坑。趁这两天有时间,整理出来并分享给你。
1
好在Ubuntu新版本,比之前的完善了n多倍,安装方便了很多,否则…,否则怎么样?-- 否则照样折腾。
Ubuntu就是不像Windows那样傻瓜化,不像Mac OS那样高大尚,才让程序员觉得更好玩,别有一番滋味在心头。
好了,闲话就说这么多,干活才是硬道理。
2
到官网下载Python3.7.2安装包,点击【Gzipped source tarball】链接,下载源代码,并解压缩到主目录下。打开【终端】,安装依赖包。
#安装GCC
sudo apt-get install build-essential
#安装ssl
sudo apt-get install openssl libssl-dev
#安装libffi
sudo apt-get install libffi-dev
#安装zlib
sudoapt-getinstallzlib1g-dev

3
编译并安装Python,默认安装在:/usr/local下。cd到Python源码目录里。
#生成Makefile文件
sudo ./configure --with-ssl
#编译程序
sudo make
#安装程序
sudo make install

安装完成后,在【终端】输入python3,确认一下。确实是改成Python 3.7.2了,并自动设置为default(默认)了。

4
安装完Python后,自带了pip3(通用的Python 包管理工具。提供了对Python 包的查找、下载、安装、卸载的功能)。很多python的大多数开发包,被统一放在Python仓库里。需要时,通过pip/pip3去获取。
在Ubuntu上获取wxPython,跟Mac OS和Windows是不同的,不是:pip3 install -U wxPython ,这样会报错,踩坑1。
#----正确命令----------
sudo pip3 install -U \-f https://extras.wxpython.org/wxPython4/extras/linux/gtk3
/ubuntu-18.04/ \wxPython

5
下载wxPython时,最后有提示pip3不是最新版的。升级命令是:sudo pip3 install --upgrade pip 。自作聪明把后面的pip改成pip3了,踩坑3。
#--------升级命令--------
sudo pip3 install –U --upgrade pip

6
假装很厉害的样子,手写一个wxPython的窗口程序。打开【文本编辑器】输入内容并保存为:main.py
# main.py
import wx
app = wx.App()
frame = wx.Frame(None, title='Hello World!')
frame.Show()
app.MainLoop()

打开【终端】,用python3运行main.py文件,看看效果。成功的,没问题。

7
打包需要有专门的工具:Pyinstaller,再次发挥pip3的力量。获取Pyinstaller并进行安装。命令是:sudo pip3 install pyinstaller。

开始打包,命令:pyinstaller –F main.py 。打包关键参数有-F、-D,它们的区别:-F打包成一个文件,-D文件分开(依赖包、资源、运行文件等)。结果报错了,提示缺少了三个依赖文件:libpython3.7mu.so.1.0,libpython3.7m.so.1.0,libpython3.7.so.1.0,踩坑4。解决方法,底部有提示,就是在编译python时,需要提供参数:--enable-shared。

8
那就重新编译吧。心若在梦就在,只不过是从头再来。你说来就来,让编译飞一会。
sudo ./configure --enable-shared
sudo make
sudo make install
稍等片刻,再次进行打包操作。涛声依旧,命令还是:sudo pyinstaller -F main.py,这下可以了么?--不行,又碉堡了。提示错误…libpython3.7m.so.1.0… No such file or directory。意思就是没有这个文件,踩坑5 。

Ubuntu下,编译依赖的文件,一般是放在/usr/lib下的。既然提示缺少文件,那就把它复制过去。打开【终端】,cd Python-3.7.2。sudo cp libpython3.7m.so.1.0 /usr/lib进行复制。

9
打开【终端】,再次运行打包命令:sudo pyinstaller -F main.py。沧海月明珠有泪,蓝田日暖玉生烟,皇天不负有心人,真不容易,终于可以了。

按捺不住喜悦,赶紧跑到主目录下的dist,一探究竟。有个文件,安静的躺在那里,等你呼喊它。双击试试。成了,成了…留下了幸福的眼泪。

相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36