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)文章
Python+PyQt5實(shí)現(xiàn)智能網(wǎng)絡(luò)驅(qū)動(dòng)器映射工具
在企業(yè)IT運(yùn)維和日常辦公環(huán)境中,網(wǎng)絡(luò)驅(qū)動(dòng)器映射是一項(xiàng)基礎(chǔ)但至關(guān)重要的功能,本文將詳細(xì)介紹如何使用Python的PyQt5庫(kù)開(kāi)發(fā)一款智能網(wǎng)絡(luò)驅(qū)動(dòng)器映射工具,有需要的可以了解下2025-06-06
Python光學(xué)仿真wxpython透鏡演示系統(tǒng)框架
這篇文章主要為大家介紹了Python光學(xué)仿真UI界面的wxpython透鏡演示系統(tǒng)框架基本講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
Python3實(shí)現(xiàn)的旋轉(zhuǎn)矩陣圖像算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的旋轉(zhuǎn)矩陣圖像算法,涉及Python3列表遍歷、切片轉(zhuǎn)換、矩陣運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Python 超時(shí)請(qǐng)求或計(jì)算的處理方案
這篇文章主要介紹了Python 超時(shí)請(qǐng)求或計(jì)算的處理方案,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
一波神奇的Python語(yǔ)句、函數(shù)與方法的使用技巧總結(jié)
這篇文章主要介紹了一波神奇的Python函數(shù)與方法的使用技巧總結(jié),包括裝飾器和with語(yǔ)句等的不常見(jiàn)用法,需要的朋友可以參考下2015-12-12
Python數(shù)據(jù)序列化技術(shù)總結(jié)
在現(xiàn)代軟件開(kāi)發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲(chǔ)或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢(shì)和適用場(chǎng)景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下2025-03-03

