今天在本地重新配了一下 ubuntu 虛擬機的環境,因爲使用的是渣校園網,下載安裝 Python 擴展庫的時候速度奇慢無比,還總是提示連接超時,下載失敗等,然後搜索了一下網上的解決辦法,個人覺得最有效的如下:
解決辦法:
1. 設置超時時間:pip --default-timeout=100 install 包名
2. 不使用緩存:pip --no-cache-dir install 包名
3. 臨時使用國內源:
pip install 包名 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
國內源列表:
- 阿里雲: https://mirrors.aliyun.com/pypi/simple/
- 中國科技大學:https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣(douban):https://pypi.douban.com/simple/
- 清華大學:https://pypi.tuna.tsinghua.edu.cn/simple/
- 中國科學技術大學:http://pypi.mirrors.ustc.edu.cn/simple/
示例:
仿照下列指令,會使下載速度得到明顯的改善:
pip install -i https://pypi.doubanio.com/simple/ 包名
或
pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt
若使用的是 python3.x 可以將指令中的“pip”修改改成“pip3”。
4. 永久使用國內源:
pip 的配置文件一般位於(如果沒有,請直接創建):
- Linux:
$HOME/.config/pip/pip.conf
- macOS:
$HOME/Library/Application Support/pip/pip.conf
- Windows:
%APPDATA%\pip\pip.ini
打開並編輯 pip 配置文件,將其中的 index-url 修改爲上面的任意一個國內源 。
配置文件示例:
[global]
index-url = https://linux.xidian.edu.cn/mirrors/pypi/web/simple/
參考文章:
解決pip安裝超時的問題 https://blog.csdn.net/qq_39161804/article/details/81191977