方法一:
適用範圍:
- 適用於單虛擬環境,因爲這種方式,會將環境中的依賴包全都加入
- 使用的全局環境,則下載的所有包都會在裏面,不管是不時當前項目依賴的
pip freeze > requirements.txt
方法二:(推薦)
適用範圍:解決方法一 的難題
pipreqs github:地址
# 安裝
pip install pipreqs
# 在當前目錄生成
pipreqs . --encoding=utf8 --force
注意 :
--encoding=utf8
爲使用utf8編碼,不然可能會報UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 406: illegal multibyte sequence 的錯誤。--force
強制執行,當 生成目錄下的requirements.txt存在時覆蓋。
使用requirements.txt安裝依賴:
pip install -r requirements.txt