文章目錄
1、嘮嘮叨叨
最近項目中需要Python的打包,看到網上也沒有很詳細的資料,於是做了一些示例程序。小小的研究了一下,Python如何在Windows和Linux上打包,並把程序源碼以及各個記錄上傳到Github上與大家分享。
2、背景
Python版本:3.6.8
Windows版本:Windows 10 家庭中文版 64-bit (10.0, Build 18362) (18362.19h1_release.190318-1202)
Linux版本:centos7.4
3、Python打包工具
今天沒時間研究cx_Freeze,先研究了一下PyInstaller。
3.1、py2exe
py2exe是一個將python轉換成windows上的可獨立執行的可執行程序(*.exe)的工具。不過,該可執行程序,只能在相同的Windows系統下運行,而且不適合Linux。果斷被我捨棄不在研究了。
3.2、cx_Freeze
cx_Freeze 是一個類似 py2exe 的工具,但 cx_Freeze 可以在 linux 下可以直接執行的 ELF 格式的二進制可執行文件,也可以在windows上執行。
cx_Freeze的作用可以讓python程序可以脫離python運行環境,在沒有安裝python的微型linux系統(例如cdlinux、tinycore等)裏,方便地運行你的python程序。
程序簡介:https://pypi.org/project/cx-Freeze/5.0/
3.3、PyInstaller
號稱是目前最全面的打包程序,然後我看了一下程序更新時間。一看是10天前,嗯,不錯,就它了。
程序簡介:https://pypi.org/project/PyInstaller/
看了一下參數介紹如下:
4、Windows打包
4.1、單個文件打包
4.1.1、核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例1,單個文件打包
“pyinstaller -F(單個可執行文件) 程序源 -n 程序名 -w(去掉控制檯窗口,這在GUI界面時非常有用) -i 圖標.ico”
“pyinstaller -F test1/Demo_Test1_Python.py”
"""
def bubble_sort(arr):
"""
冒泡排序
:param arr:
:return:
"""
for i in range(1, len(arr)):
for j in range(0, len(arr)-i):
if arr[j] > arr[j+1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
if __name__ == '__main__':
test = [1, 8, 123, 18, 99, 300]
print("************************************")
print("* 冒泡排序 *")
print("************************************")
print("源列表:", test)
result = bubble_sort(test)
print("排序後:", result)
print("************************************")
input("按任意鍵退出...")
4.1.2、程序運行
4.1.3、打包
pyinstaller -F test1/Demo_Test1_Python.py
4.1.4、打包後效果
4.2、多個文件打包
4.2.1、核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例2,多個文件打包
“pyinstaller -F(單個可執行文件) 程序源 -n 程序名 -w(去掉控制檯窗口,這在GUI界面時非常有用) -i 圖標.ico”
“pyinstaller -F test2/Demo_Test2_Python.py”
"""
from test2.Demo_bubble_sort import bubble_sort
from test2.Demo_heap_sort import heap_sort
if __name__ == '__main__':
test1 = [1, 8, 123, 18, 99, 300]
test2 = test1[:]
print("************************************")
print("* 兩個排序 *")
print("************************************")
print("列表1 id:", id(test1))
print("列表2 id:", id(test2))
print("源列表1:", test1)
print("源列表2:", test2)
result1 = bubble_sort(test1)
result2 = heap_sort(test1)
print("冒泡後:", result1)
print("堆排後:", result2)
print("************************************")
input("按任意鍵退出...")
4.2.2、程序運行
4.2.3、打包
pyinstaller -F test2/Demo_Test2_Python.py
4.2.4、打包後效果
4.3、多層文件打包
4.3.1、核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例3,多層文件打包
“pyinstaller -F(單個可執行文件) 程序源 -n 程序名 -w(去掉控制檯窗口,這在GUI界面時非常有用) -i 圖標.ico”
“pyinstaller -F test3/Demo_Test3_Python.py”
"""
from test3.sort.Demo_bubble_sort import bubble_sort
from test3.sort.Demo_heap_sort import heap_sort
from test3.Demo_test import Test
if __name__ == '__main__':
test1 = [1, 8, 123, 18, 99, 300]
test2 = test1[:]
print("************************************")
print("* 兩個排序 *")
print("************************************")
print("列表1 id:", id(test1))
print("列表2 id:", id(test2))
print("源列表1:", test1)
print("源列表2:", test2)
result1 = bubble_sort(test1)
result2 = heap_sort(test1)
print("冒泡後:", result1)
print("堆排後:", result2)
Test.run()
print("************************************")
input("按任意鍵退出...")
4.3.2、程序運行
4.3.3、打包
pyinstaller -F test3/Demo_Test3_Python.py
4.3.4、打包後效果
4.4、多層文件打包(加圖標該可執行程序名稱)
4.4.1、核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例4,多層文件打包加圖標,修改程序名
“pyinstaller -F(單個可執行文件) 程序源 -n 程序名 -w(去掉控制檯窗口,這在GUI界面時非常有用) -i 圖標.ico”
“-p 表示自定義需要加載的類路徑(一般情況下用不到)”
“pyinstaller -F test4/Demo_Test4_Python.py -n Test4 -i test4/test4.ico”
"""
from test3.sort.Demo_bubble_sort import bubble_sort
from test3.sort.Demo_heap_sort import heap_sort
from test3.Demo_test import Test
if __name__ == '__main__':
test1 = [1, 8, 123, 18, 99, 300]
test2 = test1[:]
print("************************************")
print("* 兩個排序 *")
print("************************************")
print("列表1 id:", id(test1))
print("列表2 id:", id(test2))
print("源列表1:", test1)
print("源列表2:", test2)
result1 = bubble_sort(test1)
result2 = heap_sort(test1)
print("冒泡後:", result1)
print("堆排後:", result2)
Test.run()
print("************************************")
input("按任意鍵退出...")
4.4.2、程序運行
4.4.3、打包
pyinstaller -F test4/Demo_Test4_Python.py -n Test4 -i test4/test4.ico
4.4.4、打包後效果
5、Linux打包
5.1、多層文件打包
5.1.1、核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例5,多層文件打包修改程序名 linux打包
“pyinstaller -F(單個可執行文件) 程序源 -n 程序名 -w(去掉控制檯窗口,這在GUI界面時非常有用) -i 圖標.ico”
“-p 表示自定義需要加載的類路徑(一般情況下用不到)”
“pyinstaller -F Demo_Test5_Python.py -n Test5”
"""
from sort.Demo_bubble_sort import bubble_sort
from sort.Demo_heap_sort import heap_sort
from Demo_test import Test
if __name__ == '__main__':
test1 = [1, 8, 123, 18, 99, 300]
test2 = test1[:]
print("************************************")
print("* 兩個排序 *")
print("************************************")
print("列表1 id:", id(test1))
print("列表2 id:", id(test2))
print("源列表1:", test1)
print("源列表2:", test2)
result1 = bubble_sort(test1)
result2 = heap_sort(test1)
print("冒泡後:", result1)
print("堆排後:", result2)
Test.run()
print("************************************")
input("按任意鍵退出...")
5.1.2、程序運行
5.1.3、打包後效果
5.2、遇到的錯誤,以及解決方案
5.2.1、錯誤1(找不到pyinstaller)
我是用 pip install 安裝的pyinstaller,於是先find了一下,找到了此命令,於是就做了個軟鏈接。
解決方案,直接將安裝目錄下面的pyinstaller包作爲軟鏈接到/usr/bin下
ln -s /usr/local/python3.6.8/bin/pyinstaller /usr/bin/pyinstaller3.6
5.2.2、錯誤2(rebuild your Python with --enable-shared
)
這種錯誤,人家已經把解決方案說出來了,就是需要重新編譯嘛,那我們就按照他的來就OK了。先找到源碼按照的目錄,並按照以下命令操作。
./configure --prefix=/usr/local/python3.6.8(需要安裝的目錄) --enable-shared --with-ssl
make
make install
5.2.3、錯誤3(找不到 libpython3.6m.so.1.0)
解決方案,在安裝目錄找到此文件,並拷貝到/usr/lib64目錄下: