前言
python寫的代碼如何打包成.exe可執行程序,讓別人電腦上沒安裝過 python 的小伙伴也可以直接運行?
本篇講如何用PyInstaller庫一步步打包python代碼。
PyInstaller 環境准備
我的電腦環境:
1.系統windows 10
2.python 3.6.6
PyInstaller 環境需依賴pywin32包,先安裝pywin32, 推薦pip安裝
pip install pywin32 --index-url https://pypi.douban.com/simple
如果沒安裝pywin32,后面打包會報錯:FileNotFoundError: [Errno 2] No such file or directory: 'lib\site-packages\win32\win32wnet.pyd'
使用pip安裝PyInstaller
pip install PyInstaller --index-url https://pypi.douban.com/simple
安裝完查看版本號
C:\Users\dell>pip show pywin32
Name: pywin32
Version: 228
C:\Users\dell>pip show PyInstaller
Name: pyinstaller
Version: 4.3
Requires: setuptools, importlib-metadata, altgraph, pywin32-ctypes, pyinstaller-hooks-contrib, pefile
pywin32離線包下載地址,需匹配對應的python和系統版本https://www.lfd.uci.edu/~gohlke/pythonlibs/#pip
打包.exe文件
我寫了一段簡單的 requests 代碼yoyoblog.py
"""
使用requests庫獲取我的博客首頁文章地址
上海-悠悠 blog:https://www.cnblogs.com/yoyoketang/
"""
import requests
import re
r = requests.get("https://www.cnblogs.com/yoyoketang/")
# 匹配首頁blog地址
res = re.findall(r'class="postTitle2 vertical-middle" href="(.+?)"', r.text)
for i in res:
print("blog:", i)
命令行運行結果
D:\demo\myblog>python yoyoblog.py
blog: https://www.cnblogs.com/yoyoketang/p/14811325.html
blog: https://www.cnblogs.com/yoyoketang/p/14500093.html
blog: https://www.cnblogs.com/yoyoketang/p/14084401.html
blog: https://www.cnblogs.com/yoyoketang/p/10302295.html
blog: https://www.cnblogs.com/yoyoketang/p/14891200.html
blog: https://www.cnblogs.com/yoyoketang/p/14891142.html
blog: https://www.cnblogs.com/yoyoketang/p/14890723.html
blog: https://www.cnblogs.com/yoyoketang/p/14890583.html
blog: https://www.cnblogs.com/yoyoketang/p/14888404.html
blog: https://www.cnblogs.com/yoyoketang/p/14887668.html
blog: https://www.cnblogs.com/yoyoketang/p/14887087.html
blog: https://www.cnblogs.com/yoyoketang/p/14884606.html
blog: https://www.cnblogs.com/yoyoketang/p/14873194.html
blog: https://www.cnblogs.com/yoyoketang/p/14869348.html
這段代碼依賴第三方庫requests,需在python目錄Lib\site-packages下找到requests
找到后整個文件夾全部復制到代碼根目錄
cd到項目跟目錄myblog下執行命令
pyinstaller -F yoyoblog.py
執行過程如下
D:\demo\myblog>pyinstaller -F yoyoblog.py
275 INFO: PyInstaller: 4.3
275 INFO: Python: 3.6.6
275 INFO: Platform: Windows-10-10.0.17134-SP0
276 INFO: wrote D:\demo\myblog\yoyoblog.spec
...
14480 INFO: Writing RT_ICON 7 resource with 1128 bytes
14486 INFO: Updating manifest in D:\demo\myblog\build\yoyoblog\run.exe.l_fhvf3j
14486 INFO: Updating resource type 24 name 1 language 0
14490 INFO: Appending archive to EXE D:\demo\myblog\dist\yoyoblog.exe
16213 INFO: Building EXE from EXE-00.toc completed successfully.
看到successfully 就是成功了
pyinstaller 一些常用參數命令
- -F 表示生成單個可執行文件
- -w 表示去掉控制台窗口,這在GUI界面時非常有用。不過如果是命令行程序的話那就把這個選項刪除吧!
- -p 表示你自己自定義需要加載的類路徑,一般情況下用不到
- -i 表示可執行文件的圖標
更多參數可以通過pyinstaller -h
查看
執行.exe文件
打包完成后會在 dist 目錄看到一個 yoyoblog.exe 文件
可以打開到對應目錄
雙擊運行yoyoblog.exe
雙擊運行發現代碼運行很快,一閃就沒有了,這是因為代碼運行完就自動結束了,導致啥都沒看到。
解決辦法1:可以在cmd窗口執行.exe文件
D:\demo\myblog\dist>yoyoblog.exe
blog: https://www.cnblogs.com/yoyoketang/p/14811325.html
blog: https://www.cnblogs.com/yoyoketang/p/14500093.html
blog: https://www.cnblogs.com/yoyoketang/p/14084401.html
blog: https://www.cnblogs.com/yoyoketang/p/10302295.html
blog: https://www.cnblogs.com/yoyoketang/p/14891200.html
blog: https://www.cnblogs.com/yoyoketang/p/14891142.html
blog: https://www.cnblogs.com/yoyoketang/p/14890723.html
blog: https://www.cnblogs.com/yoyoketang/p/14890583.html
blog: https://www.cnblogs.com/yoyoketang/p/14888404.html
blog: https://www.cnblogs.com/yoyoketang/p/14887668.html
blog: https://www.cnblogs.com/yoyoketang/p/14887087.html
blog: https://www.cnblogs.com/yoyoketang/p/14884606.html
blog: https://www.cnblogs.com/yoyoketang/p/14873194.html
blog: https://www.cnblogs.com/yoyoketang/p/14869348.html
解決辦法2:加一句代碼按任意鍵退出
"""
使用requests庫獲取我的博客首頁文章地址
上海-悠悠 blog:https://www.cnblogs.com/yoyoketang/
"""
import requests
import re
r = requests.get("https://www.cnblogs.com/yoyoketang/")
# 匹配首頁blog地址
res = re.findall(r'class="postTitle2 vertical-middle" href="(.+?)"', r.text)
for i in res:
print("blog:", i)
input("press any key to exit!")
重新執行pyinstaller -F yoyoblog.py
打包,這樣就可以雙擊運行了
icon 制作
-i
參數打包的時候可以自定義icon圖標
-i <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">, --icon <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">
FILE.ico: apply that icon to a Windows executable.
FILE.exe,ID, extract the icon with ID from an exe.
FILE.icns: apply the icon to the .app bundle on Mac OS
X. Use "NONE" to not apply any icon, thereby making
the OS to show some default (default: apply
PyInstaller's icon)
先找一張icon圖片放到項目跟目錄(注意並不是每個圖片格式都可以,必須是icon格式)
加 -i
參數打包
pyinstaller -F yoyoblog.py -i favicon.ico
打包完成重新雙擊運行,會看到左上角有自己的icon了
icon在線制作https://www.bitbug.net/