windows、linux下打包Python3程序詳細(xì)方法
最近項(xiàng)目中需要Python的打包,看到網(wǎng)上也沒(méi)有很詳細(xì)的資料,于是做了一些示例程序。研究了一下,Python如何在Windows和Linux上打包
背景
Python版本:3.6.
Windows版本:Windows 10 家庭中文版 64-bit (10.0, Build 18362) (18362.19h1_release.190318-1202)
Linux版本:centos7.
Python打包工具
今天沒(méi)時(shí)間研究cx_Freeze,先研究了一下PyInstaller。
py2exe
py2exe是一個(gè)將python轉(zhuǎn)換成windows上的可獨(dú)立執(zhí)行的可執(zhí)行程序(*.exe)的工具。不過(guò),該可執(zhí)行程序,只能在相同的Windows系統(tǒng)下運(yùn)行,而且不適合Linux。果斷被我舍棄不在研究了。
cx_Freeze
cx_Freeze 是一個(gè)類似 py2exe 的工具,但 cx_Freeze 可以在 linux 下可以直接執(zhí)行的 ELF 格式的二進(jìn)制可執(zhí)行文件,也可以在windows上執(zhí)行。
cx_Freeze的作用可以讓python程序可以脫離python運(yùn)行環(huán)境,在沒(méi)有安裝python的微型linux系統(tǒng)(例如cdlinux、tinycore等)里,方便地運(yùn)行你的python程序。
程序簡(jiǎn)介:https://pypi.org/project/cx-Freeze/5.0/
PyInstaller
號(hào)稱是目前最全面的打包程序,然后我看了一下程序更新時(shí)間。一看是10天前,嗯,不錯(cuò),就它了。
程序簡(jiǎn)介:https://pypi.org/project/PyInstaller/
看了一下參數(shù)介紹如下:

Windows打包Python程序?qū)嵗?/h2>
1、單個(gè)文件打包
核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例1,單個(gè)文件打包
“pyinstaller -F(單個(gè)可執(zhí)行文件) 程序源 -n 程序名 -w(去掉控制臺(tái)窗口,這在GUI界面時(shí)非常有用) -i 圖標(biāo).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("按任意鍵退出...")
程序運(yùn)行

打包方法
pyinstaller -F test1/Demo_Test1_Python.py
打包后效果

多個(gè)文件打包
核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例2,多個(gè)文件打包
“pyinstaller -F(單個(gè)可執(zhí)行文件) 程序源 -n 程序名 -w(去掉控制臺(tái)窗口,這在GUI界面時(shí)非常有用) -i 圖標(biāo).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("* 兩個(gè)排序 *")
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("按任意鍵退出...")
程序運(yùn)行

打包命令
pyinstaller -F test2/Demo_Test2_Python.py
打包后效果

多層文件打包
核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例3,多層文件打包
“pyinstaller -F(單個(gè)可執(zhí)行文件) 程序源 -n 程序名 -w(去掉控制臺(tái)窗口,這在GUI界面時(shí)非常有用) -i 圖標(biāo).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("* 兩個(gè)排序 *")
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("按任意鍵退出...")
程序運(yùn)行

打包命令
pyinstaller -F test4/Demo_Test4_Python.py -n Test4 -i test4/test4.ico
打包后效果

Python Linux打包實(shí)例
多層文件打包
核心源碼
#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create Time: 2020-1-20
Info: Python打包示例5,多層文件打包修改程序名 linux打包
“pyinstaller -F(單個(gè)可執(zhí)行文件) 程序源 -n 程序名 -w(去掉控制臺(tái)窗口,這在GUI界面時(shí)非常有用) -i 圖標(biāo).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("* 兩個(gè)排序 *")
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("按任意鍵退出...")
程序運(yùn)行

打包后效果

遇到的錯(cuò)誤,以及解決方案
錯(cuò)誤1(找不到pyinstaller)
我是用 pip install 安裝的pyinstaller,于是先f(wàn)ind了一下,找到了此命令,于是就做了個(gè)軟鏈接。
解決方案,直接將安裝目錄下面的pyinstaller包作為軟鏈接到/usr/bin下
ln -s /usr/local/python3.6.8/bin/pyinstaller /usr/bin/pyinstaller3.
錯(cuò)誤2(rebuild your Python with --enable-shared)

這種錯(cuò)誤,人家已經(jīng)把解決方案說(shuō)出來(lái)了,就是需要重新編譯嘛,那我們就按照他的來(lái)就OK了。先找到源碼按照的目錄,并按照以下命令操作。
./configure --prefix=/usr/local/python3.6.8(需要安裝的目錄) --enable-shared --with-ssl make make install
錯(cuò)誤3(找不到 libpython3.6m.so.1.0)
解決方案,在安裝目錄找到此文件,并拷貝到/usr/lib64目錄下:
本文主要講解了如何在windows與linux下打包Python3程序的詳細(xì)方法,更多關(guān)于打包Python程序的知識(shí)請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
詳解pytest中runtestprotocol方法的實(shí)現(xiàn)
runtestprotocol 是 pytest 執(zhí)行測(cè)試流程中的一個(gè)核心函數(shù),它主要負(fù)責(zé)調(diào)用測(cè)試函數(shù)的“setup”、“call”和“teardown”鉤子函數(shù),并生成對(duì)應(yīng)的測(cè)試報(bào)告,本文將深入探究pytest中runtestprotocol方法的實(shí)現(xiàn),需要的朋友可以參考下2023-10-10
TensorFlow設(shè)置日志級(jí)別的幾種方式小結(jié)
今天小編就為大家分享一篇TensorFlow設(shè)置日志級(jí)別的幾種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹(shù)的高效且可擴(kuò)展實(shí)現(xiàn)
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹(shù)的高效且可擴(kuò)展實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
基于Python和Scikit-Learn的機(jī)器學(xué)習(xí)探索
這篇文章主要介紹了基于Python和Scikit-Learn的機(jī)器學(xué)習(xí)探索的相關(guān)內(nèi)容,小編覺(jué)得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友學(xué)習(xí)和參考。2017-10-10
python+django加載靜態(tài)網(wǎng)頁(yè)模板解析
這篇文章主要介紹了python+django加載靜態(tài)網(wǎng)頁(yè)模板解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
解決Django模板無(wú)法使用perms變量問(wèn)題的方法
這篇文章主要給大家介紹了關(guān)于解決Django模板無(wú)法使用perms變量問(wèn)題的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

