教學 3 - 分發包裝¶
到目前為止,我們一直在開發人員模式下運行我們的應用程式。這使我們可以輕鬆地在本地運行我們的應用程式 - 但我們真正想要的是能夠將我們的應用程式提供給其他人。
但是,我們不想教導使用者如何安裝 Python、建立虛擬環境、複製 git 儲存庫以及在開發人員模式下執行 Briefcase。我們寧願只給他們一個安裝程序,然後讓應用程式正常工作。
公文包可用於打包您的應用程式以透過這種方式進行分發。
創建您的應用程式支架¶
由於這是我們第一次打包應用程序,因此我們需要創建一些配置文件和其他腳手架來支援打包過程。從helloworld目錄中,運行:
(beeware-venv) $ briefcase create
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-macOS-app-template.git, branch v0.3.18
...
[helloworld] Installing support package...
...
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Created build/helloworld/macos/app
(beeware-venv) $ briefcase create
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-linux-AppImage-template.git, branch v0.3.18
...
[helloworld] Installing support package...
No support package required.
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Created build/helloworld/linux/ubuntu/jammy
有關 Python 版本的錯誤
如果您收到類似以下的錯誤:
執行 Briefcase 所使用的 Python 版本 (3.12) 並非系統 python3 (3.10)。
您需要使用系統 python3重新建立虛擬環境。使用系統 Python 是打包應用程式的必要條件。
(beeware-venv) C:\...>briefcase create
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-windows-app-template.git, branch v0.3.18
...
[helloworld] Installing support package...
...
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Created build\helloworld\windows\app
您可能剛剛在終端機中看到了內容頁面……那麼剛剛發生了什麼?公事包做了以下事情:
- 它**生成一個應用程式範本。除了您實際應用程式的程式碼之外,建構原生安裝程式還需要許多檔案和設定。除了正在建構的實際應用程式的名稱之外,這些額外的腳手架對於相同平台上的每個應用程式幾乎都是一樣的
-
因此 Briefcase 為其支援的每個平台提供應用程式範本。此步驟會推出範本,代換您的應用程式名稱、bundle ID,以及配置檔案的其他屬性,以支援您正在建置的平台。如果您對 Briefcase 提供的範本不滿意,您可以提供自己的範本。但是,在使用 Briefcase 預設範本累積更多經驗之前,您可能不想這麼做。
-
它下載並安裝支援套件。Briefcase 採用的封裝方式最適合形容為「可能可行的最簡單方式」- 它將完整、獨立的 Python 解譯器,作為它建立的每個應用程式的一部分 (除了 Linux 原生系統套件建立之外)。這會稍微降低空間效率 - 如果您有 5 個應用程式使用 Briefcase 套件,您就會有 5 份 Python 解譯器。但是,這種方法保證每個應用程式都是完全獨立的,使用已知可與應用程式搭配使用的特定 Python 版本。同樣地,Briefcase 為每個平台提供預設的支援套件;如果您願意,可以提供您自己的支援套件,並將該套件包含在建立程序中。如果您在 Python 解譯器中有需要啟用的特定選項,或如果您想要從標準函式庫中刪除在執行時不需要的模組,您可能想要這樣做。Briefcase 維護支援套件的本機快取,因此一旦您下載了特定的支援套件,該快取複本將在未來的建立中使用。如上所述,當 Briefcase 將應用程式打包為原生 Linux 系統套件 (Linux 的預設套件格式),支援套件不會隨應用程式一起包含。取而代之,應用程式會使用目標 Linux 發行版所提供的 Python。
-
它安裝了應用程式要求。您的應用程式可以指定運行時所需的任何第三方模組。這些將使用
pip安裝到應用程式的安裝程式中。 -
它安裝了您的應用程式代碼。您的應用程式將擁有自己的程式碼和資源(例如,運行時所需的映像);這些檔案被複製到安裝程式中。
-
它 安裝了應用程式所需的資源。 最後,它添加了安裝程式本身所需的任何其他資源。這包括需要附加到最終應用程式的圖示和啟動螢幕圖像等內容。
完成後,如果您查看專案目錄,您現在應該會看到與您的平台(macOS、linux或windows)相對應的目錄,其中包含其他檔案。這是您的應用程式的特定於平台的打包配置。
建立您的應用程式¶
現在您可以編譯您的應用程式。此步驟執行應用程式在目標平台上可執行所需的任何二進位編譯。
(beeware-venv) $ briefcase build
[helloworld] Adhoc signing app...
...
Signing build/helloworld/macos/app/Hello World.app
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07
[helloworld] Built build/helloworld/macos/app/Hello World.app
在 macOS 上,build 命令不需要 編譯 任何內容,但它確實需要對二進位內容進行簽名,以便可以執行。此簽名是 臨時 簽名 - 它僅適用於
您的 機器;如果您想將應用程式分發給其他人,則需要提供完整的簽名。
(beeware-venv) $ briefcase build
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Building application...
Build bootstrap binary...
make: Entering directory '/home/brutus/beeware-tutorial/helloworld/build/linux/ubuntu/jammy/bootstrap'
...
make: Leaving directory '/home/brutus/beeware-tutorial/helloworld/build/linux/ubuntu/jammy/bootstrap'
Building bootstrap binary... done
Installing license... done
Installing changelog... done
Installing man page... done
Updating file permissions... done
Stripping binary... done
[helloworld] Built build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/bin/helloworld
此步驟完成後,build資料夾將包含一個helloworld-0.0.1資料夾,其中包含
Linux/usr檔案系統的鏡像。該檔案系統鏡像將包含一個包含helloworld二進位檔案的bin資料夾,以及支援該二進位檔案所需的lib和share資料夾。
(beeware-venv) C:\...>briefcase build
Setting stub app details... done
[helloworld] Built build\helloworld\windows\app\src\Hello World.exe
在 Windows 上,build 命令不需要 編譯 任何內容,但它確實需要編寫一些元數據,以便應用程式知道其名稱、版本等。
觸發防毒軟體
由於此元資料在create命令期間直接寫入從範本推出的預編譯二進位檔案中,因此這可能會觸發電腦上執行的防毒軟體並阻止寫入元資料。在這種情況下,指示防毒軟體允許該工具(名為rcedit-x64.exe)運行並重新執行上述命令。
運行您的應用程式¶
現在您可以使用 Briefcase 來運行您的應用程式:
(beeware-venv) $ briefcase run
[helloworld] Starting app...
===========================================================================
Configuring isolated Python...
Pre-initializing Python runtime...
PythonHome: /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
PYTHONPATH:
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python311.zip
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib/lib-dynload
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app_packages
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app
Configure argc/argv...
Initializing Python runtime...
Installing Python NSLog handler...
Running app module: helloworld
---------------------------------------------------------------------------
(beeware-venv) $ briefcase run
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Starting app...
===========================================================================
Install path: /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr
Pre-initializing Python runtime...
PYTHONPATH:
- /usr/lib/python3.10
- /usr/lib/python3.10/lib-dynload
- /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/lib/helloworld/app
- /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/lib/helloworld/app_packages
Configure argc/argv...
Initializing Python runtime...
Running app module: helloworld
---------------------------------------------------------------------------
(beeware-venv) C:\...>briefcase run
[helloworld] Starting app...
===========================================================================
Log started: 2023-04-23 04:47:45Z
PreInitializing Python runtime...
PythonHome: C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src
PYTHONPATH:
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\python39.zip
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app_packages
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app
Configure argc/argv...
Initializing Python runtime...
Running app module: helloworld
---------------------------------------------------------------------------
這將開始使用build命令的輸出來執行您的本機應用程式。
您可能會注意到應用程式運行時的外觀存在一些細微的差異。例如,作業系統顯示的圖示和名稱可能與您在開發人員模式下運行時看到的圖示略有不同。這也是因為您正在使用打包的應用程序,而不僅僅是運行
Python 程式碼。從作業系統的角度來看,您現在運行的是應用程式,而不是Python 程式,這反映在應用程式的顯示方式上。
繼續之前,請關閉應用程式。與之前的教學步驟一樣,您可以按下應用程式視窗上的關閉按鈕、從應用程式的功能表中選擇 Quit/Exit,或在執行「briefcase run」的終端輸入 Ctrl+C。
建立您的安裝程式¶
現在,您可以使用package命令打包您的應用程式以進行分發。 package
命令執行將鷹架專案轉換為最終的可分發產品所需的任何編譯。根據平台的不同,這可能涉及編譯安裝程式、執行程式碼簽署或執行其他預分發任務。
(beeware-venv) $ briefcase package --adhoc-sign
[helloworld] Signing app...
*************************************************************************
** WARNING: Signing with an ad-hoc identity **
*************************************************************************
This app is being signed with an ad-hoc identity. The resulting
app will run on this computer, but will not run on anyone else's
computer.
To generate an app that can be distributed to others, you must
obtain an application distribution certificate from Apple, and
select the developer identity associated with that certificate
when running 'briefcase package'.
*************************************************************************
Signing app with ad-hoc identity...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07
[helloworld] Building DMG...
Building dist/Hello World-0.0.1.dmg
[helloworld] Packaged dist/Hello World-0.0.1.dmg
dist 資料夾將包含一個名為Hello World-0.0.1.dmg 的檔案。如果您在 Finder 中找到此文件,然後雙擊其圖標,您將安裝
DMG,為您提供 Hello World 應用程式的副本以及指向您的應用程式資料夾的鏈接,以便於安裝。將應用程式檔案拖曳到應用程式中,您就已經安裝了應用程式。將
DMG 檔案發送給朋友,他們應該能夠執行相同的操作。
在此範例中,我們使用了--adhoc-sign選項 - 也就是說,我們使用 ad hoc 憑證簽署我們的應用程式 -
只能在您的電腦上使用的臨時憑證。我們這樣做是為了讓教程簡單。設定程式碼簽署身分有點繁瑣,並且只有在您打算將應用程式分發給其他人時才需要它們。如果我們要發布真實的應用程式供其他人使用,我們需要指定真實的憑證。
當您準備好發布實際應用程式時,請查看有關`設定 macOS 代碼簽署身分 的公文包操作指南
根據您的 Linux 發行版,打包步驟的輸出將略有不同。如果您使用的是 Debian 派生的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Building .deb package...
Write Debian package control file... done
dpkg-deb: building package 'helloworld' in 'helloworld-0.0.1.deb'.
Building Debian package... done
[helloworld] Packaged dist/helloworld_0.0.1-1~ubuntu-jammy_amd64.deb
dist 資料夾將包含產生的.deb 檔案。
如果您使用的是基於 RHEL 的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting fedora:40 (Vendor base rhel)
Determining glibc version... done
Targeting glibc 2.39
Targeting Python3.12
[helloworld] Building .rpm package...
Generating rpmbuild layout... done
Write RPM spec file... done
Building source archive... done
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.Kav9H7
+ umask 022
...
+ exit 0
Building RPM package... done
[helloworld] Packaged dist/helloworld-0.0.1-1.fc40.x86_64.rpm
dist 資料夾將包含產生的.rpm 檔案。
如果您使用的是基於 Arch 的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting arch:20240101 (Vendor base arch)
Determining glibc version... done
Targeting glibc 2.38
Targeting Python3.12
[helloworld] Building .pkg.tar.zst package...
...
Building Arch package... done
[helloworld] Packaged dist/helloworld-0.0.1-1-x86_64.pkg.tar.zst
dist 資料夾將包含產生的.pkg.tar.zst 檔案。
目前不支援打包其他 Linux 發行版。
如果您想為您正在使用的 Linux 發行版以外的發行版建立軟體包,Briefcase 也可以提供協助 - 但您需要安裝 Docker。
Docker Engine 的官方安裝程式可用於一系列 Unix
發行版。請遵循適合您平台的說明;但是,請確保不要以無根模式安裝 Docker。
安裝 Docker 後,您應該能夠啟動 Linux 容器 - 例如:
$ docker run --rm -it ubuntu:22.04
將在 Ubuntu 22.04 Docker 容器內向您顯示 Unix 提示字元(類似root@844444e31cff9:/#)。鍵入 Ctrl-D 退出
Docker 並返回本機 shell。
安裝 Docker 後,您可以透過傳入 Docker 映像作為參數,使用 Briefcase 為 Briefcase 支援的任何 Linux 發行版建置套件。例如,要為 Ubuntu 22.04 (Jammy) 建立 DEB 包,無論您使用什麼作業系統,都可以運行:
$ briefcase package --target ubuntu:jammy
這將為您選擇的作業系統下載 Docker 映像,建立一個能夠運行 Briefcase 建置的容器,並在映像內建置應用程式包。完成後,dist
資料夾將包含目標 Linux 發行版的軟體包。
(beeware-venv) C:\...>briefcase package
*************************************************************************
** WARNING: No signing identity provided **
*************************************************************************
Briefcase will not sign the app. To provide a signing identity,
use the `--identity` option; or, to explicitly disable signing,
use `--adhoc-sign`.
*************************************************************************
[helloworld] Building MSI...
Compiling application manifest...
Compiling... done
Compiling application installer...
helloworld.wxs
helloworld-manifest.wxs
Compiling... done
Linking application installer...
Linking... done
[helloworld] Packaged dist\Hello_World-0.0.1.msi
在此範例中,我們使用了--adhoc-sign選項 - 也就是說,我們使用 ad hoc 憑證簽署我們的應用程式 -
只能在您的電腦上使用的臨時憑證。我們這樣做是為了讓教程簡單。設定程式碼簽署身分有點繁瑣,並且只有在您打算將應用程式分發給其他人時才需要它們。如果我們要發布真實的應用程式供其他人使用,我們需要指定真實的憑證。
當您準備好發布實際應用程式時,請查看有關`設定 macOS 代碼簽署身分 的公文包操作指南
此步驟完成後,dist資料夾將包含一個名為 Hello_World-0.0.1.msi 的檔案。如果您雙擊此安裝程式來運行它,您應該經歷熟悉的
Windows 安裝過程。安裝完成後,開始功能表中將出現一個 "Hello World" 項目。
下一步¶
我們現在已將應用程式打包,以便在桌面平台上發行。但是,當我們需要更新應用程式中的程式碼時該怎麼辦?我們該如何將這些更新置入已打包的應用程式中?請參閱 教學 4 來找出答案…