---
GA: G-7GF24SD6DM
---
## Python開發筆記:requitement.txt 輸出相依的套件函式庫
### PIP.Freeze:輸出產生 Requirements.txt
這邊會產生整個環境上所用到函式庫,也就是`pip list`的整個Output。
```shell
pip freeze > requirements.txt
```
### Pipreqs:特定專案中所用到的函式庫輸出Requirements.txt
本次介紹一個可以只輸出特定專案中,所用到的函式庫輸出就好。
這樣就不用將每個程式會用到的函式庫,或不相干的函式庫也被納入到Requirements.txt檔案裡了。
也可以更方便直接看到整理的SBOM。
[Pipreqs](https://github.com/bndr/pipreqs),是開源套件,以[Apache-2.0](https://github.com/bndr/pipreqs/blob/master/LICENSE)做授權,可以直接透過pip做安裝。
```shell=
$ pip install pipreqs
Collecting pipreqs
Downloading pipreqs-0.5.0-py3-none-any.whl.metadata (7.9 kB)
Requirement already satisfied: docopt==0.6.2 in /usr/lib/python3/dist-packages (from pipreqs) (0.6.2)
Collecting ipython==8.12.3 (from pipreqs)
Downloading ipython-8.12.3-py3-none-any.whl.metadata (5.7 kB)
Collecting nbconvert<8.0.0,>=7.11.0 (from pipreqs)
Downloading nbconvert-7.16.4-py3-none-any.whl.metadata (8.5 kB)
Collecting yarg==0.1.9 (from pipreqs)
......
Installing collected packages: pickleshare, fastjsonschema, backcall, yarg, pyzmq, pandocfilters, mistune, jupyterlab-pygments, jupyter-core, jupyter-client, ipython, nbformat, nbclient, nbconvert, pipreqs
Attempting uninstall: ipython
Found existing installation: ipython 8.20.0
ERROR: Cannot uninstall ipython 8.20.0, RECORD file not found. Hint: The package was installed by debian.
# 最後一行提示有個函式可能透過OS安裝的,此處應該可以不用處置
```
安裝完成後,便可開始使用。
最簡單使用方式是`pipreqs PATH`,`PATH`填入欲解析之專案。
pip安裝的套件因不在OS的PATH(終端使用路徑)中,但我也不想弄一弄搞混,所以這邊採用不加入OS Path方式處理。
```shell=
$ python -m pipreqs.pipreqs ./
INFO: Not scanning for jupyter notebooks.
WARNING: requirements.txt already exists, use --force to overwrite it
# 此處提示同資料夾下已有requirements.txt檔案,可以用 --force 強制覆蓋寫入。
$ python -m pipreqs.pipreqs ./ --force
INFO: Not scanning for jupyter notebooks.
WARNING: Import named "nmap" not found locally. Trying to resolve it at the PyPI server.
WARNING: Package "nmap" does not exist or network problems
WARNING: Import named "rich" not found locally. Trying to resolve it at the PyPI server.
WARNING: Import named "rich" was resolved to "rich:13.7.1" package (https://pypi.org/project/rich/).
Please, verify manually the final list of requirements.txt to avoid possible dependency confusions.
INFO: Successfully saved requirements file in ./requirements.txt
```
看到最後一行表示已完成建立檔案。
### Requirements.txt 文件內容
基本上會由套件名稱與需求版本內容編寫而成,內容大概如下:
```shell=
NetfilterQueue==1.1.0
rich==13.7.1
scapy==2.5.0+git20240324.2b58b51
```
前面套件名稱,`==`表示需求版本等於多少。
以第一行為例,需要`NetfilterQueue`這項套件,版本號碼須等於`1.1.0`。
你也可以編輯一下。
版本號碼也可以指定其他方式,例如:
| Code | ps. | Example Code |
| ---- | --- | ------------ |
| `>=` | 大於等於多少版本號 | `>=1.1.0` |
| `>=xxx,<=xxx` | 大於等於多少,小於等於多少版本號 | `>=1.0.8,<=1.1.0` |
### 透過Requirements.txt檔案讀取,安裝相依套件
```shell
pip install -r requirements.txt
```
### 額外說明:PIP套件更新
建議先檢查PIP版本:
```shell=
pip -version
pip -v
```
查看PIP已安裝套件清單:
```shell=
pip list
```
PIP套件本體需要更新:
```shell=
pip install --upgrade pip
pip install -U pip
```
-----
### FAQ
Q1. 為何要使用Requirements.txt?
A1. 便於將程式碼、專案進行打包,下次使用時可以透過Requirements.txt直接一次性使用指令將其相依套件一次安裝。不必一一輸入指令安裝。
Q2. PIP套件版本號碼建議如何定義?
A2. 程式碼基本不像硬體需要年年更新,但有時驅動會更新,舊程式可能無法執行。建議以能維持就維持現有執行的版本號,除非該版本可能為較早的先行版會與時俱進再一一更新。反正依然需要使用者自行判定。
Q3. 有使用的套件不在Requirements.txt?
A3. 如是使用LinuxOS,可能另外安裝會在APT中(筆者常用Debian系列,其他系列自行判斷),請另外在系統的套件庫中安裝。
-----
Copyright ChanYuRick 2024. [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/)