在開發 Python app 或是 Python library 時
很重要的一件事情是 紀錄這一個 application 使用了哪些第三方的模組
當我們使用 Virtualenv 或 venv (Python3)
從一個乾淨的虛擬環境下 透過 pip 安裝所需要的第三方模組
pip freeze 會列出所有被安裝的 python 模組及其版本
$ pip freeze > requirements.txt
例如
meld==3.14.2
pip-autoremove==0.9.0
virtualenv==15.0.1
未來要重現環境時 輸入
$ pip -r requirements.txt
就可以取得相同的環境
這做法相當適合 在 production 的環境下運行的 application
然而這一個做法有幾個缺點
- 不易升級所使用的 module
- 不易看出 此專案所依賴的 top dependencies
為了解決這樣的問題 一個簡單的做法是 維護兩份文件
- requirements.txt
- requirements-to-freeze.txt
其中 requirements-to-freeze.txt 紀錄你的專案所依賴的 top-level dependencies
requirements.txt 紀錄 pip freeze 的結果
範例
$ cd project-repo
$ pip install -r requirements-to-freeze.txt --upgrade
$ pip freeze > requirements.txt
Misc
nvie/pip-tools https://github.com/nvie/pip-tools