1. 程式人生 > 程式設計 >Python更換pip源方法過程解析

Python更換pip源方法過程解析

  在使用Python時,需要使用各種各樣的庫,通常會使用pip直接安裝,這樣最為簡單也最方便。但最為崩潰的地方在於有時候速度出奇的慢,因為pip預設使用的源為官方源,而官方源在國外。通常的解決方法是更換源,常見的國內源如下所示:

https://pypi.tuna.tsinghua.edu.cn/simple/ 清華
http://pypi.doubanio.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
https://pypi.mirrors.ustc.edu.cn/simple/ 中國科學技術大學
http://mirrors.163.com/pypi/simple/ 網易

Windows下永久更換源

1.在執行視窗或資源管理器中輸入%APPDATA%

Python更換pip源方法過程解析

2.進入目錄後,新建一個資料夾pip,並在該資料夾裡面新建檔案pip.ini,並輸入以下內容:

Python更換pip源方法過程解析

  文字內容如下所示:

[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

3.下次再使用pip安裝的時候,就可以更換為國內的pip源。

Linux下永久更換源

1.在家目錄中建立.pip目錄

mkdir -p ~/.pip

2.建立pip.conf檔案

vim ~/.pip/pip.conf

3.輸入以下內容並儲存

[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

臨時更改pip源

pip install <包名> -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn

  • -i https://pypi.tuna.tsinghua.edu.cn/simple 表示使用清華源
  • --trusted-host pypi.tuna.tsinghua.edu.cn 表示新增信任

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。