實際開發中,我們的項目部署環境可能是封閉的內網環境,無法直接使用pip install -r requirement.txt這種方式安裝項目依賴包,這時pip download這個犀利的工具就要發光發熱了
1. 使用pip download在一個可以連接外網的環境下載整個項目的依賴包
pip download \ --only-binary=:all: \ # 對於包以及包的依賴包,都不使用二進制 --platform linux_x86_64 \ # 指定系統環境 -d \home\packs # 下載的文件的存儲目錄 -r requirement.txt # 指定要下載的包
備注:實際使用中,會發現一些包找不到,比如tornado,會報錯如下
ERROR: Could not find a version that satisfies the requirement tornado==6.0 (from -r src/requirements.txt (line 13)) (from versions: none) ERROR: No matching distribution found for tornado==6.0 (from -r src/requirements.txt (line 13))
原因:沒有提供符合條件的二進制包
解決方法:使用非二進制包安裝 --no-binary=:all: package_name
pip download --no-binary=:all: tornado==6.0 -d pkg/
2. 使用這些下載好的包
sudo pip3 install -r /xxxx/xxxx/requirement.txt --no-index --find-links /xxxx/xxxxx/xxxxx/(存放下載好的包的目錄)