1、臨時使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
2、永久更改pip源
升級 pip 到最新的版本 (>=10.0.0) 后進行配置:
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
如果您到 pip 默認源的網絡連接較差,臨時使用鏡像站來升級 pip:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
其它源:
阿里雲:https://mirrors.aliyun.com/pypi/simple/
豆瓣:https://pypi.douban.com/simple/
北京外國語大學 https://mirrors.bfsu.edu.cn/pypi/web/simple/
3、離線安裝單個依賴包
下載單個依賴包(在有外部網絡的環境下進行)
pip3 download -d /home/package_whl/ requests -i https://pypi.douban.com/simple
單個依賴包安裝(將剛剛下載好的幾個.whl文件拷貝到目標服務器,該服務器可以是內部局域網,不能訪問外部網絡)
pip3 install requests-2.24.0-py2.py3-none-any.whl
4、離線安裝python3及依賴包
將pip3 list的信息生成文檔
pip3 freeze >requirements.txt
將requirement.txt文檔中列出的模塊信息下載到指定目錄
pip3 download -r requirements.txt -d /tmp/packages/ #推薦使用
或
pip3 install --download /tmp/packages -r requirements.txt
將下載好的模塊copy到離線服務器
pip3 install xxx.tar.gz
pip3 install xxx.whl
pip3 install xxx.xx #是什么格式就安裝什么格式的文件即可。
如果有要安裝的包和依賴包有多個,且不知道先裝哪個,那么就把這些文件放在一個目錄中,然后進入該目錄使用下面命令一起安裝
pip3 install ./*
批量離線安裝requirments.txt中的模塊,需要將下載好的模塊和requirments.txt都copy到一個目錄,然后執行下面的命令
pip3 install --no-index --find-links=/tmp/packages -r requirments.txt
常見錯誤解決:
一、setuptools 新版本安裝時總是遇到這樣那樣的問題,查閱發現,新版本廢棄了一些原來使用的依賴,如:
setuptools 工具包在58版本之后,廢棄了use_2to3 方法。
解決方法:使用舊版本的setuptools
pip3 install setuptools==57.5.0
二、下載依賴包再安裝移到其它電腦總是出錯
解決方案:在有網的情況下建議使用網絡下載安裝 requirements.txt
pip3 install -r requirements.txt