python 國內鏡像加速


原因

經常在使用Python的時候需要安裝各種模塊,而pip是很強大的模塊安裝工具
但是由於國外官方默認pip訪問速度慢,經常被牆,導致無法安裝,所以我們最好是將自己使用的pip源更換一下,這樣就能解決被牆導致的裝不上庫的煩惱

# 查看已經安裝軟件列表
$ pip freeze
# 查看當前配置
$ python -m site

 

USER_BASE 和 USER_SITE 其實就是用戶自定義的啟用Python腳本和依賴安裝包的基礎路徑

鏡像列表

http://mirrors.aliyun.com/pypi/simple/ //阿里
https://pypi.tuna.tsinghua.edu.cn/simple/ //清華
http://pypi.douban.com/ //豆瓣
http://pypi.hustunique.com/ //華中理工大學
http://pypi.sdutlinux.org/ //山東理工大學
http://pypi.mirrors.ustc.edu.cn/ //中國科學技術大學

國內使用得比較多並且速度比較快的是阿里的pip源或者清華大學,清華大學的是官網pypi的鏡像

臨時使用

pip的時候加參數 -i

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gevent

常駐使用

Linux or macOS

修改 ~/.pip/pip.conf (沒有就自己創建), 增加或者修改 index-url 至源

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
disable-pip-version-check = true
  • 豆瓣源使用
mkdir -p ~/.pip
echo -e '[global]\ntrusted-host = pypi.douban.com\nindex-url = http://pypi.douban.com/simple' > ~/.pip/pip.conf

windows

直接在user目錄中創建一個pip目錄,如:C:\Users\xx\pip

新建文件 pip.ini 內容

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
disable-pip-version-check = true
timeout = 120

或者使用豆瓣源

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
disable-pip-version-check = true
timeout = 120
  • index-url 源,可以換成其他的源
  • trusted-host 添加源為可信主機,要不然可能報錯
  • disable-pip-version-check 設置為true取消pip版本檢查,排除每次都報最新的pip
  • timeout 超時設置
mkdir -p ~/.pip
echo -e '[global]\ntrusted-host = pypi.douban.com\nindex-url = http://pypi.douban.com/simple' > ~/.pip/pip.conf

windows

直接在user目錄中創建一個pip目錄,如:C:\Users\xx\pip

新建文件 pip.ini 內容

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
disable-pip-version-check = true
timeout = 120

或者

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
disable-pip-version-check = true
timeout = 120

pip conf 參數說明

  • [install] 安裝參數
  • install-option=--prefix= pip install的安裝路徑
  • [global] 表示為全局配置
  • index-url 源,可以換成其他的源
  • trusted-host 添加源為可信主機,要不然可能報錯
  • disable-pip-version-check 設置為true取消pip版本檢查,排除每次都報最新的pip
  • timeout 超時設置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM