iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 21

[Python]打包用pyinstaller和 virtualenv

  • 分享至 

  • xImage
  •  

pyinstaller用在平常完成專案進度做打包的動作,但是沒有注意到環境設訂的很容易打包些不必要的python函式庫,造成生成的執行檔體積過大,不必要的函式庫是其他專案所留下的,為了把自己正在執行進度的專案和其他專案所使用的函式作區隔,於是我需要另一個工具virtualenv,顧名思義是指在系統中生成另一個乾淨虛擬環境並安裝對應專案的函式庫再打包就好了。
https://pypi.org/project/pyinstaller/

pip3 install pyinstaller

安裝pyinstaller訊息

Collecting pyinstaller
  Downloading pyinstaller-4.0.tar.gz (3.5 MB)
     |████████████████████████████████| 3.5 MB 656 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting pyinstaller-hooks-contrib>=2020.6
  Downloading pyinstaller_hooks_contrib-2020.9-py2.py3-none-any.whl (163 kB)
     |████████████████████████████████| 163 kB 3.2 MB/s
Collecting pywin32-ctypes>=0.2.0; sys_platform == "win32"
  Using cached pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
Collecting altgraph
  Using cached altgraph-0.17-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: setuptools in c:\python38\lib\site-packages (from pyinstaller) (47.1.0)
Collecting pefile>=2017.8.1; sys_platform == "win32"
  Using cached pefile-2019.4.18.tar.gz (62 kB)
Collecting future
  Using cached future-0.18.2.tar.gz (829 kB)
Using legacy setup.py install for pefile, since package 'wheel' is not installed.
Using legacy setup.py install for future, since package 'wheel' is not installed.
Building wheels for collected packages: pyinstaller
  Building wheel for pyinstaller (PEP 517) ... done
  Created wheel for pyinstaller: filename=pyinstaller-4.0-py3-none-any.whl size=2789243 sha256=956a2274a87878cfafa47bc860d3836b530e1ce23d6904c089c2d7cbedb446f4
  Stored in directory: c:\users\wifipc\appdata\local\pip\cache\wheels\9b\28\8f\f51ae22c4391bbeb36c8fdc68203b0c094445b4db824a6ab7d
Successfully built pyinstaller
Installing collected packages: pyinstaller-hooks-contrib, pywin32-ctypes, altgraph, future, pefile, pyinstaller
    Running setup.py install for future ... done
    Running setup.py install for pefile ... done
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.0 pyinstaller-hooks-contrib-2020.9 pywin32-ctypes-0.2.0

https://pypi.org/project/virtualenv/

pip3 install virtualenv

安裝virtualenv

Collecting virtualenv
  Downloading virtualenv-20.0.33-py2.py3-none-any.whl (4.9 MB)
     |████████████████████████████████| 4.9 MB 3.2 MB/s
Collecting filelock<4,>=3.0.0
  Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting distlib<1,>=0.3.1
  Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
     |████████████████████████████████| 335 kB 1.6 MB/s
Requirement already satisfied: six<2,>=1.9.0 in c:\python38\lib\site-packages (from virtualenv) (1.15.0)
Collecting appdirs<2,>=1.4.3
  Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Installing collected packages: filelock, distlib, appdirs, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 virtualenv-20.0.33

製造一個虛擬環境env_win

C:\Users\WiFiPC\Documents\nice_device_implement>virtualenv env_win
created virtual environment CPython3.8.5.final.0-64 in 4125ms
  creator CPython3Windows(dest=C:\Users\WiFiPC\Documents\nice_device_implement\env_win, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\WiFiPC\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.2.3, setuptools==50.3.0, wheel==0.35.1
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

進入env_win\Scripts, 呼叫activate,(env_win)被呼叫起來

C:\Users\WiFiPC\Documents\nice_device_implement\env_win\Scripts>activate

(env_win) C:\Users\WiFiPC\Documents\nice_device_implement\env_win\Scripts>

我的環境rely設定
requirements.txt

backcall==0.2.0
bcrypt==3.2.0
cffi==1.14.2
colorama==0.4.3
cryptography==3.0
decorator==4.4.2
ffmpeg==1.4
ipython==7.17.0
ipython-genutils==0.2.0
jedi==0.17.2
numpy==1.19.1
opencv-python==4.4.0.42
paramiko==2.7.1
paramiko-expect==0.2.8
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==7.2.0
prompt-toolkit==3.0.6
ptyprocess==0.6.0
pycparser==2.20
Pygments==2.6.1
PyNaCl==1.4.0
scp==0.13.2
six==1.15.0
traitlets==4.3.3
wcwidth==0.2.5

在(env_win)中安裝rely
pip3 install -r requirements.txt

Collecting backcall==0.2.0
  Using cached backcall-0.2.0-py2.py3-none-any.whl (11 kB)
Collecting bcrypt==3.2.0
  Using cached bcrypt-3.2.0-cp36-abi3-win_amd64.whl (28 kB)
Collecting cffi==1.14.2
  Using cached cffi-1.14.2-cp38-cp38-win_amd64.whl (179 kB)
Collecting colorama==0.4.3
  Using cached colorama-0.4.3-py2.py3-none-any.whl (15 kB)
Collecting cryptography==3.0
  Using cached cryptography-3.0-cp38-cp38-win_amd64.whl (1.5 MB)
Collecting decorator==4.4.2
  Using cached decorator-4.4.2-py2.py3-none-any.whl (9.2 kB)
Collecting ffmpeg==1.4
  Using cached ffmpeg-1.4.tar.gz (5.1 kB)
Collecting ipython==7.17.0
  Using cached ipython-7.17.0-py3-none-any.whl (786 kB)
Collecting ipython-genutils==0.2.0
  Using cached ipython_genutils-0.2.0-py2.py3-none-any.whl (26 kB)
Collecting jedi==0.17.2
  Using cached jedi-0.17.2-py2.py3-none-any.whl (1.4 MB)
Collecting numpy==1.19.1
  Using cached numpy-1.19.1-cp38-cp38-win_amd64.whl (13.0 MB)
Collecting opencv-python==4.4.0.42
  Using cached opencv_python-4.4.0.42-cp38-cp38-win_amd64.whl (33.5 MB)
Collecting paramiko==2.7.1
  Using cached paramiko-2.7.1-py2.py3-none-any.whl (206 kB)
Collecting paramiko-expect==0.2.8
  Using cached paramiko_expect-0.2.8-py2.py3-none-any.whl (11 kB)
Collecting parso==0.7.1
  Using cached parso-0.7.1-py2.py3-none-any.whl (109 kB)
Collecting pexpect==4.8.0
  Using cached pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
Collecting pickleshare==0.7.5
  Using cached pickleshare-0.7.5-py2.py3-none-any.whl (6.9 kB)
Collecting Pillow==7.2.0
  Using cached Pillow-7.2.0-cp38-cp38-win_amd64.whl (2.1 MB)
Collecting prompt-toolkit==3.0.6
  Using cached prompt_toolkit-3.0.6-py3-none-any.whl (354 kB)
Collecting ptyprocess==0.6.0
  Using cached ptyprocess-0.6.0-py2.py3-none-any.whl (39 kB)
Collecting pycparser==2.20
  Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Collecting Pygments==2.6.1
  Using cached Pygments-2.6.1-py3-none-any.whl (914 kB)
Collecting PyNaCl==1.4.0
  Using cached PyNaCl-1.4.0-cp38-cp38-win_amd64.whl (206 kB)
Collecting scp==0.13.2
  Using cached scp-0.13.2-py2.py3-none-any.whl (9.5 kB)
Collecting six==1.15.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting traitlets==4.3.3
  Using cached traitlets-4.3.3-py2.py3-none-any.whl (75 kB)
Collecting wcwidth==0.2.5
  Using cached wcwidth-0.2.5-py2.py3-none-any.whl (30 kB)
Requirement already satisfied: setuptools>=18.5 in c:\users\wifipc\documents\nice_device_implement\env_win\lib\site-packages (from ipython==7.17.0->-r requirements.txt (line 8)) (50.3.0)
Building wheels for collected packages: ffmpeg
  Building wheel for ffmpeg (setup.py) ... done
  Created wheel for ffmpeg: filename=ffmpeg-1.4-py3-none-any.whl size=6087 sha256=4e960a596df02660f2e9561271ae24d6e1f1a485040c3e6624c5c9c75b403097
  Stored in directory: c:\users\wifipc\appdata\local\pip\cache\wheels\30\33\46\5ab7eca55b9490dddbf3441c68a29535996270ef1ce8b9b6d7
Successfully built ffmpeg
Installing collected packages: backcall, six, pycparser, cffi, bcrypt, colorama, cryptography, decorator, ffmpeg, ipython-genutils, traitlets, parso, jedi, pickleshare, wcwidth, prompt-toolkit, Pygments, ipython, numpy, opencv-python, PyNaCl, paramiko, paramiko-expect, ptyprocess, pexpect, Pillow, scp
Successfully installed Pillow-7.2.0 PyNaCl-1.4.0 Pygments-2.6.1 backcall-0.2.0 bcrypt-3.2.0 cffi-1.14.2 colorama-0.4.3 cryptography-3.0 decorator-4.4.2 ffmpeg-1.4 ipython-7.17.0 ipython-genutils-0.2.0 jedi-0.17.2 numpy-1.19.1 opencv-python-4.4.0.42 paramiko-2.7.1 paramiko-expect-0.2.8 parso-0.7.1 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.6 ptyprocess-0.6.0 pycparser-2.20 scp-0.13.2 six-1.15.0 traitlets-4.3.3 wcwidth-0.2.5

pyinstaller常用選項

-F, --onefile
只建立一個單獨的可執行檔案(否則會生成很多依賴檔案)

-c, --console, --nowindowed
建立一個命令列程式(就是那個小黑框),這是預設的選項

-w, --windowed, --noconsole
不彈出命令列,如果你寫的是圖形介面的程式可以使用此選項

-i,–icon
為程式指定一個圖示,需要跟上圖示的路徑

我使用的icon檔:
https://ithelp.ithome.com.tw/upload/images/20201006/20119608XyXoacVFtn.png

(env_win) C:\Users\WiFiPC\Documents\nice_device_implement\Tkinter_GUI\release>pyinstaller --noconsole --hidden-import='PIL._tkinter_finder'  --onefile -i icons\screen_webcam_web_camera_icon_148791.ico nice_device_Test_Tool.py

https://ithelp.ithome.com.tw/upload/images/20201006/20119608zLJnPFcAE1.png
https://ithelp.ithome.com.tw/upload/images/20201006/20119608DyTf3BZBOZ.png


上一篇
[Python]使用pytube3下載youtube影片
下一篇
[Python]How to Extract Image Metadata in Python
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
jakeuj
iT邦新手 5 級 ‧ 2024-06-17 18:18:35

想問一下先安裝 pyInstaller (全域)
再建立虛擬環境後,怎麼呼叫 pyinstaller?
ChatGPT
當你切換到 Python 的虛擬環境後,該虛擬環境會擁有獨立的 Python 解釋器和套件安裝位置。這意味著,默認情況下,你在全域(global)環境中安裝的工具和套件在虛擬環境中是不可用的。

我要留言

立即登入留言