Python設置pip鏡像源的幾種方法
在 Python 中使用 pip 設置鏡像源可以顯著提升包下載速度,特別是在國內網絡環(huán)境下。以下是幾種設置方法:
1. 臨時使用鏡像源
在 pip 安裝命令中直接指定鏡像源:
# 使用清華鏡像源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ package-name # 使用阿里云鏡像源 pip install -i https://mirrors.aliyun.com/pypi/simple/ package-name # 使用豆瓣鏡像源 pip install -i https://pypi.douban.com/simple/ package-name # 使用中科大鏡像源 pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ package-name
2. 永久配置鏡像源
Windows 系統
在用戶目錄下創(chuàng)建 pip 文件夾:
- 路徑:
C:\Users\用戶名\pip\
創(chuàng)建 pip.ini 文件,添加以下內容:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn timeout = 6000
Linux/macOS 系統
- 創(chuàng)建或修改配置文件:
# 創(chuàng)建目錄 mkdir -p ~/.pip # 創(chuàng)建或修改配置文件 vim ~/.pip/pip.conf
- 添加以下內容:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn timeout = 6000
3. 使用命令配置
# 設置清華源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/ pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn # 設置阿里云源 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ pip config set global.trusted-host mirrors.aliyun.com
4. 查看當前配置
# 查看所有配置 pip config list # 查看指定配置項 pip config get global.index-url
5. 常用國內鏡像源
# 清華鏡像源 https://pypi.tuna.tsinghua.edu.cn/simple/ # 阿里云鏡像源 https://mirrors.aliyun.com/pypi/simple/ # 豆瓣鏡像源 https://pypi.douban.com/simple/ # 中科大鏡像源 https://pypi.mirrors.ustc.edu.cn/simple/ # 華為云鏡像源 https://repo.huaweicloud.com/repository/pypi/simple/
6. 恢復默認源
如果需要恢復官方源:
# 刪除配置文件 # Windows: 刪除 C:\Users\用戶名\pip\pip.ini # Linux/macOS: 刪除 ~/.pip/pip.conf # 或者使用命令重置 pip config unset global.index-url
7. 使用多個鏡像源
如果需要配置多個鏡像源作為備份:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url =
https://mirrors.aliyun.com/pypi/simple/
https://pypi.douban.com/simple/
trusted-host =
pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
pypi.douban.com
注意事項
- 信任主機:對于非官方源,需要設置
trusted-host參數 - 超時設置:可以適當增加
timeout值避免下載超時 - SSL 驗證:如果遇到 SSL 問題,可以添加
--trusted-host參數
推薦使用清華鏡像源或阿里云鏡像源,它們在國內的訪問速度和穩(wěn)定性都比較好。
到此這篇關于Python設置pip鏡像源的幾種方法的文章就介紹到這了,更多相關Python設置pip鏡像源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決已經安裝requests,卻依然提示No module named requests問題
今天小編就為大家分享一篇解決已經安裝requests,卻依然提示No module named 'requests'問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
單步調試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調試 step into/step out/step over 區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Python筆記之Scipy.stats.norm函數使用解析
這篇文章主要介紹了Python筆記之Scipy.stats.norm函數使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

