1. 程式人生 > 其它 >pip 和 Conda 映象站配置

pip 和 Conda 映象站配置

如果你經常使用 Python,那麼你對 pip 和 Conda 一定不陌生,它們作為包管理器,可以非常方便的幫助我們下載需要的 Python 包,但是受限於大多 Python 包的伺服器在國外,國內下載速度緩慢,因此需要配置映象站提升下載速度。

本文首發於 正切橙的部落格,更多詳細資訊請點選 這裡,轉載請註明出處。


一、pip 映象站

1.pip 是什麼

pip 是一個通用的 Python 包管理器,具有對 Python 包查詢、下載、安裝、解除安裝的功能。pip 已內置於 Python3.4 和 2.7 及以上版本中。pip 預設從 PyPI 中下載包,PyPI 全名為 Python Package Index

,是 Python 的正式第三方(official third-party)軟體包的軟體儲存庫。

PyPI 官網

2.映象站列表

name index-url trusted-host
阿里雲 https://mirrors.aliyun.com/pypi/simple/ mirrors.aliyun.com
豆瓣 https://pypi.douban.com/simple/ pypi.douban.com
騰訊雲 https://mirrors.cloud.tencent.com/pypi/simple/ mirrors.cloud.tencent.com
華為雲 https://repo.huaweicloud.com/repository/pypi/simple/
repo.huaweicloud.com
清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/ pypi.tuna.tsinghua.edu.cn
北京外國語大學 https://mirrors4.bfsu.edu.cn/pypi/web/simple/ mirrors4.bfsu.edu.cn
PyPI https://pypi.org/ pypi.org

不同地區訪問映象站的速度可能不同,請自行選擇合適的映象站

中科大的 pip 和 Conda 映象站會重定向到北京外國語大學映象站,故不列舉

3.如何配置

檢視 pip 安裝源資訊

pip config list

①.臨時使用

每次使用 pip 安裝包時指定映象站

pip install [package-name] -i [index-url] --trusted-host [trusted-host]

例如安裝 tensorflow 時使用 阿里雲 映象站

pip install tensorflow -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

②.永久使用

全域性使用映象站,所有包都通過該映象站下載

pip config set global.index-url [index-url]
pip config set install.trusted-host [trusted-host]

例如全域性使用 阿里雲 映象站

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

③.詳細配置

Windows

全域性使用映象站後,會在 C:\Users\xxx\AppData\Roaming\pip 中產生一個 pip.ini 檔案(C:\Users\xxx\AppData\Roaming\pip\pip.ini)來記錄配置資訊(如果不存在就自己建立),我們可以修改這個檔案進行更詳細的配置,下面有一個參考模板:

[global]
timeout = 600
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url = https://pypi.douban.com/simple/
            https://pypi.org/
                  
[install]
trusted-host = mirrors.aliyun.com
            pypi.douban.com
            pypi.org
  • timeout = 600:超時限制為 600 秒

Linux/macOS

全域性使用映象站後,會在使用者根目錄 ~ 中產生一個隱藏資料夾 .pip,其中的 pip.conf 檔案(~/.pip/pip.conf)來記錄配置資訊(如果不存在就自己建立),具體內容和 Windows 配置一樣

二、Conda 映象站

1.Conda 是什麼

Conda 是一個開源的軟體包和環境管理系統,用於安裝多個版本的軟體包及其依賴關係,並在它們之間輕鬆切換。它的包管理與 pip 類似,可以用來管理 Python 的第三方包。

Conda 官網

2.映象站列表

name channels
阿里雲 https://mirrors.aliyun.com/anaconda/
清華大學 https://mirrors.tuna.tsinghua.edu.cn/anaconda/
北京外國語大學 https://mirrors.bfsu.edu.cn/anaconda/

不同地區訪問映象站的速度可能不同,請自行選擇合適的映象站

3.如何配置

檢視 Conda 安裝源資訊

conda info

①.全域性使用

conda config --add channels [channels]

例如全域性新增 清華大學 映象站

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/

②.詳細配置

Windows

全域性使用映象站後,會在 C:\Users\xxx 中產生一個隱藏檔案 .condarcC:\Users\xxx\.condarc)來記錄配置資訊(如果不存在就自己建立),我們可以修改這個檔案進行更詳細的配置,下面有一個參考模板:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r/
  - defaults
ssl_verify: true
show_channel_urls: true
auto_activate_base: false

上述模板展示了 Conda 主要庫的映象源,Conda 附加庫的源可以通過 映象站列表 中的地址訪問映象站後自行新增,例如從 清華大學 映象站中新增的 pytorch 源地址為 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

  • ssl_verify: true:開啟 SSH 認證
  • show_channel_urls: true:安裝包時,顯示包的源地址
  • auto_activate_base: false:關閉自動進入 base 環境

Linux/macOS

全域性使用映象站後,會在使用者根目錄 ~ 中產生一個隱藏檔案 .condarc~/.condarc)來記錄配置資訊(如果不存在就自己建立),具體內容和 Windows 配置一樣