1.臨時換源:
#清華源 pip install markdown -i https://pypi.tuna.tsinghua.edu.cn/simple # 阿里源 pip install markdown -i https://mirrors.aliyun.com/pypi/simple/ # 騰訊源 pip install markdown -i http://mirrors.cloud.tencent.com/pypi/simple # 豆瓣源 pip install markdown -i http://pypi.douban.com/simple/
2.永久換源:
# 清華源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # 阿里源 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ # 騰訊源 pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple # 豆瓣源 pip config set global.index-url http://pypi.douban.com/simple/
# 換回默認源
pip config unset global.index-url
pip換源的方式
在使用Python安裝包工具pip時經常會出現下載很慢的情況,這其中有很大一部分原因和pip的源有關,在我們安裝python后,通常python解釋器自帶pip這個工具,但是這里pip是設置的默認源,也就是官方源:
https://pypi.org/simple
,這個源在國內的下載速度是很慢的(精通FQ的大神另說),所以我們為了提高包的下載速度我們可以通過換源來實現。
PYPI國內源路徑
-
豆瓣(douban) http://pypi.douban.com/simple/
換源方式
這里我們提供兩種換源的方式:
- 臨時換源
- 永久換源
臨時換源
臨時換源只需要在pip安裝包時,加上一個-i
參數后接源的url即可:
# 下載python中的Django包,這里使用的是豆瓣源
pip install django -i http://pypi.douban.com/simple
顯然不是一個一勞永逸的方法,只有下少量包的時候有使用的場景,下面我要介紹永久換源的方法,通過這個方式換源,那么以后我們下載的包就可以全部從這個url中下載了,這樣大大減輕了我們的工作量,明顯比臨時換源的方法更好。
永久換源(更換默認源)
Linux
-
在根目錄下創建/修改
~/.pip/pip.conf
pip配置文件; -
進入文件新增/修改內容;
[global] index-url=http://pypi.douban.com/simple [install] trusted-host=pypi.douban.com
-
保存文件並退出;
Windows
- windows在%HOMEPATH%\pip\pip.ini中修改上面第二步的內容;(例如:C:\Users\hp\AppData\Roaming\pip\pip.ini)
- 保存文件退出;
常見問題
-
安裝包的時候出現
Collecting beautifulsoup4 The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with ‘–trusted-host mirrors.aliyun.com’. Could not find a version that satisfies the requirement beautifulsoup4 (from versions: ) No matching distribution found for beautifulsoup4
這是一個問題是在pip鏡像升級報警,只需要添加信任源即可:
-
臨時換源處理
pip install beautifulsoup4 --trusted-host mirrors.aliyun.com
-
更換默認源配置(一勞永逸)
[install] trusted-host=pypi.douban.com
-