Python生成requirements.txt方法
在查看別人的Python項目時,經常會看到一個requirements.txt文件,里面記錄了當前程序的所有依賴包及版本號,其作用是用來在另一個環境上重新構建項目所需要的運行環境依賴。
方法一:
requirements.txt可以通過pip命令自動生成和安裝,這種情況更適用於此項目是單獨的虛擬python環境
生成requirements.txt文件
pip freeze > requirements.txt
安裝requirements.txt依賴
pip install -r requirements.txt
方法二:
使用pipreqs github地址
CTOLib碼庫對這個模塊的描述是:從真實的代碼挖掘出項目真正導入的模塊, 並組織為 pip requirements.txt
安裝
pip install pipreqs
用法
pipreqs /home/project/location
以下摘自github:
Why not pip freeze?
pip freeze only saves the packages that are installed with pip install in your environment.
pip freeze saves all packages in the environment including those that you don't use in your current project. (if you don't have virtualenv)
and sometimes you just need to create requirements.txt for a new project without installing modules.