Python設(shè)置pip鏡像源的幾種方法
在 Python 中使用 pip 設(shè)置鏡像源可以顯著提升包下載速度,特別是在國(guó)內(nèi)網(wǎng)絡(luò)環(huán)境下。以下是幾種設(shè)置方法:
1. 臨時(shí)使用鏡像源
在 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 系統(tǒng)
在用戶目錄下創(chuàng)建 pip 文件夾:
- 路徑:
C:\Users\用戶名\pip\
創(chuàng)建 pip.ini 文件,添加以下內(nèi)容:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn timeout = 6000
Linux/macOS 系統(tǒng)
- 創(chuàng)建或修改配置文件:
# 創(chuàng)建目錄 mkdir -p ~/.pip # 創(chuàng)建或修改配置文件 vim ~/.pip/pip.conf
- 添加以下內(nèi)容:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn timeout = 6000
3. 使用命令配置
# 設(shè)置清華源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/ pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn # 設(shè)置阿里云源 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ pip config set global.trusted-host mirrors.aliyun.com
4. 查看當(dāng)前配置
# 查看所有配置 pip config list # 查看指定配置項(xiàng) pip config get global.index-url
5. 常用國(guó)內(nèi)鏡像源
# 清華鏡像源 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. 恢復(fù)默認(rèn)源
如果需要恢復(fù)官方源:
# 刪除配置文件 # Windows: 刪除 C:\Users\用戶名\pip\pip.ini # Linux/macOS: 刪除 ~/.pip/pip.conf # 或者使用命令重置 pip config unset global.index-url
7. 使用多個(gè)鏡像源
如果需要配置多個(gè)鏡像源作為備份:
[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
注意事項(xiàng)
- 信任主機(jī):對(duì)于非官方源,需要設(shè)置
trusted-host參數(shù) - 超時(shí)設(shè)置:可以適當(dāng)增加
timeout值避免下載超時(shí) - SSL 驗(yàn)證:如果遇到 SSL 問(wèn)題,可以添加
--trusted-host參數(shù)
推薦使用清華鏡像源或阿里云鏡像源,它們?cè)趪?guó)內(nèi)的訪問(wèn)速度和穩(wěn)定性都比較好。
到此這篇關(guān)于Python設(shè)置pip鏡像源的幾種方法的文章就介紹到這了,更多相關(guān)Python設(shè)置pip鏡像源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決已經(jīng)安裝requests,卻依然提示No module named requests問(wèn)題
今天小編就為大家分享一篇解決已經(jīng)安裝requests,卻依然提示No module named 'requests'問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python中Pytest測(cè)試框架的fixture使用詳解
這篇文章主要介紹了Python中Pytest測(cè)試框架的fixture使用詳解,Pytest的fixture的目的是提供一個(gè)測(cè)試的基線,在此基線基礎(chǔ)上,可以更可靠的進(jìn)行重復(fù)測(cè)試,需要的朋友可以參考下2023-08-08
Python簡(jiǎn)單定義與使用字典dict的方法示例
這篇文章主要介紹了Python簡(jiǎn)單定義與使用字典的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了Python字典的原理、組成、定義及使用方法,需要的朋友可以參考下2017-07-07
利用Python輕松實(shí)現(xiàn)視頻轉(zhuǎn)GIF動(dòng)圖
在看視頻的時(shí)候覺得某段非常有意思想弄成動(dòng)圖,但是無(wú)從下手!本文就將介紹如何利用Python搞定這一需求,感興趣的小伙伴可以學(xué)習(xí)一下2022-01-01
單步調(diào)試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調(diào)試 step into/step out/step over 區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python筆記之Scipy.stats.norm函數(shù)使用解析
這篇文章主要介紹了Python筆記之Scipy.stats.norm函數(shù)使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

