[Python] 使用 pip 下載套件檔案,並離線安裝
平常安裝 Python 套件,都是直接使用 pip,
像是 pip install flask 就能安裝 Flask~
不過今天遇到一台機器它連不到 Internet,
所以不能用平常的 pip 用法來安裝套件…
查了一下,還是有方法的~
1. 用 pip download 下載相關檔案
例如用 pip download flask 把 Flask 相關的檔案抓下來,
最好是放在一個目錄裡面,方便待會傳到要安裝的電腦上:
testuser@network ~/flask_files $ pip download flask Collecting flask Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB) 100% |################################| 92kB 901kB/s Saved ./Flask-1.0.2-py2.py3-none-any.whl Collecting Werkzeug>=0.14 (from flask) Downloading https://files.pythonhosted.org/packages/18/79/84f02539cc181cdbf5ff5a41b9f52cae870b6f632767e43ba6ac70132e92/Werkzeug-0.15.2-py2.py3-none-any.whl (328kB) 100% |################################| 337kB 5.0MB/s Saved ./Werkzeug-0.15.2-py2.py3-none-any.whl Collecting click>=5.1 (from flask) Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB) 100% |################################| 81kB 11.9MB/s Saved ./Click-7.0-py2.py3-none-any.whl Collecting itsdangerous>=0.24 (from flask) Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl Saved ./itsdangerous-1.1.0-py2.py3-none-any.whl Collecting Jinja2>=2.10 (from flask) Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB) 100% |################################| 133kB 7.3MB/s Saved ./Jinja2-2.10.1-py2.py3-none-any.whl Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask) Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl Saved ./MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl Successfully downloaded flask Werkzeug click itsdangerous Jinja2 MarkupSafe
這邊要注意的是,執行 pip download 的電腦,
最好與要被安裝的電腦使用相同的作業系統版本,
以免 pip 抓給這個作業系統的檔案,可能無法在另外的作業系統使用~
2. 使用 pip 從本地目錄安裝套件
想辦法將剛剛下載下來的目錄,傳到要安裝的電腦上。
接著用 pip install 指令來安裝,只是要搭配兩個參數:
- –no-index:只看 –find-links 參數指定的位置
- –find-links:指定套件檔案位置,可以指定本地的一個目錄
如下,使用 pip install 搭配參數,就能把 Flask 安裝起來了:
root@nonetwork /root # pip install --no-index --find-links flask_files flask Looking in links: flask_files Collecting flask Collecting Werkzeug>=0.14 (from flask) Collecting click>=5.1 (from flask) Collecting itsdangerous>=0.24 (from flask) Collecting Jinja2>=2.10 (from flask) Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask) Installing collected packages: Werkzeug, click, itsdangerous, MarkupSafe, Jinja2, flask Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.2 click-7.0 flask-1.0.2 itsdangerous-1.1.0
參考資料:pip – Python Packages Offline Installation – Stack Overflow
(本頁面已被瀏覽過 10,409 次)