四步教你學(xué)會打包一個新的Python模塊
當你安裝一個應(yīng)用程序時,你通常是在安裝一個軟件包,其中包含應(yīng)用程序的可執(zhí)行代碼和重要文件,如文檔、圖標等。在 Linux上,軟件一般被打包成 RPM 或 DEB 等格式,用戶只要通過 ? ?dnf?
?? 或者 ? ?apt?
?? 等命令就可以進行安裝了,這取決于你使用的 Linux 發(fā)行版。然而幾乎每天都有新的 Python 模塊發(fā)布,因此你很容易遇到一個尚未打包的 Python 模塊。這就是 ? ?pyp2rpm?
? 存在的意義了。
最近我在嘗試安裝一個叫 ? ?python-concentration?
? 的模塊,但是進展并不太順利:
$ sudo dnf install python-concentration Updating Subscription Management repositories. Last metadata expiration check: 1:23:32 ago on Sat 11 Jun 2022 06:37:25. No match for argument: python-concentration Error: Unable to find a match: python-concentration
雖然這是一個發(fā)布在 PyPi 的包,但它仍不能被打包成 RPM 包。好消息是你可以使用 ? ?pyp2rpm?
? 以一個相對簡單的過程將它打包成 RPM 包。
首先你需要設(shè)置兩個目錄:
$ mkdir rpmbuild $ cd rpmbuild && mkdir SPECS
像這樣去安裝 ? ?pyp2rpm?
?:
$ sudo dnf install pyp2rpm
1、生成 spec 文件
RPM 包的基礎(chǔ)是一種 spec 文件,這個文件包含你創(chuàng)建這個包的所有信息,如所需的依賴關(guān)系、應(yīng)用的版本號、安裝的文件等信息。當指向某個 Python 模塊時,? ?pyp2rpm?
? 會為它構(gòu)建一個 spec 文件,你可以用它來創(chuàng)建 RPM 包。
下面以 ? ?python-concentration?
? 為例演示如何構(gòu)建一個 spec 文件:
$ pyp2rpm concentration > ~/rpmbuild/SPECS/concentration.spec
下面是它生成的文件:
# Created by pyp2rpm-3.3.8 %global pypi_name concentration %global pypi_version 1.1.5 Name: python-%{pypi_name} Version: %{pypi_version} Release: 1%{?dist} Summary: Get work done when you need to, goof off when you don't License: None URL: None Source0: %{pypi_source} BuildArch: noarch BuildRequires: python3-devel BuildRequires: python3dist(setuptools) %description Concentration [ >= 2.6.1 with python3dist(hug) < 3~~) Requires: python3dist(setuptools) %description -n python3-%{pypi_name} Concentration [需要記錄日期。
%changelog * Sat Jun 11 2022 Tux <tux@example.com> - 1.1.5-1
再次運行 ? ?rpmint?
?:
$ rpmlint ~/rpmbuild/SPEC/concentration.spec 0 packages and 1 specfiles checked; 0 errors, 0 warnings.
成功!
3、下載源碼
你需要下載好打包的代碼才能進一步構(gòu)建 RPM 包。一種簡單的方式是解析你的 spec 文件以獲取源碼的網(wǎng)址。
首先,通過 ? ?dnf?
?? 安裝 ? ?spectool?
?:
$ sudo dnf install spectool
然后通過 ? ?spectool?
? 來下載源碼:
$ cd ~/rpmbuild $ spectool -g -R SPEC/concentration.spec Downloading: https://files.pythonhosted.org/...concentration-1.1.5.tar.gz 6.0 KiB / 6.0 KiB [=====================================] Downloaded: concentration-1.1.5.tar.gz
這樣就創(chuàng)建了一個 ? ?SOURCES?
? 目錄并將源碼放入其中。
4、構(gòu)建源軟件包
現(xiàn)在你已經(jīng)驗證過 spec 文件了,接下來就可以通過 ? ?rpmbuild?
?? 構(gòu)建源軟件包了。如果你還沒有安裝 ? ?rpmbuild?
??,你也可以通過 ? ?dnf?
?? 安裝 ? ?rpm-build?
?? 包(或者在使用 ? ?rpmbuild?
? 命令時根據(jù)終端的的提示進行安裝)。
參數(shù) ? ?-bs?
? 表示構(gòu)建源軟件包。添加這個參數(shù)會產(chǎn)生一個 src.rpm 文件,這是一個用于為特定架構(gòu)重新構(gòu)建的通用包:
$ rpmbuild -bs SPECS/concentration.spec Wrote: ~/rpmbuild/SRPMS/python-concentration-1.1.5-1.el9.src.rpm
為你的系統(tǒng)構(gòu)建一個可安裝的 RPM 文件:
$ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm error: Failed build dependencies: python3-devel is needed by python-concentration-1.1.5-1.el9.noarch
看起來這個包需要安裝 Python 的開發(fā)庫才能繼續(xù)構(gòu)建。安裝它們以繼續(xù)構(gòu)建。這一次,構(gòu)建成功了,并且渲染了更多的輸出(為了清楚起見,我在這里簡略了輸出):
$ sudo dnf install python3-devel -y $ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm [...] Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.TYA7l2 + umask 022 + cd /home/bogus/rpmbuild/BUILD + rm -rf concentration-1.1.5 + RPM_EC=0 ++ jobs -p + exit 0
你的 RPM 包現(xiàn)在已經(jīng)構(gòu)建在 ? ?RPMS?
?? 子目錄下,像平常一樣使用 ? ?dnf?
? 安裝它。
$ sudo dnf install RPMS/noarch/python3-concentration*rpm
為什么不使用 PyPi
通常情況下我們并不需要將 Python 模塊打包成 RPM 包。通過 PyPi 來安裝模塊也是可以接受的,但是 PyPi 會安裝額外的包管理器對你的模塊進行檢查和更新。當你使用 ? ?dnf?
?? 來安裝 RPM 包時,你在安裝完成時就能夠獲取到完整的安裝列表。有了 ? ?pyp2rpm?
? 之后,這個過程就變得快速、簡單且自動化了。
到此這篇關(guān)于四步教你學(xué)會打包一個新的Python模塊的文章就介紹到這了,更多相關(guān)Python打包模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python的tkinter布局之簡單的聊天窗口實現(xiàn)方法
這篇文章主要介紹了python的tkinter布局之簡單的聊天窗口實現(xiàn)方法,對于tkinter用法做了初步的介紹與應(yīng)用展示,需要的朋友可以參考下2014-09-09opencv+playwright滑動驗證碼的實現(xiàn)
滑動驗證碼是常見的驗證碼之一,本文主要介紹了opencv+playwright滑動驗證碼的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-11-11