程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

pip and conda use Tsinghua mirror to speed up python package downloads

編輯:Python

攜手創作,共同成長!這是我參與「掘金日新計劃 · 8 月更文挑戰」的第4天,點擊查看活動詳情

導語

聽說最近Pytorch支持MacBook M1Running on the chip is accelerated,我想在MacBook上試一下,結果在使用pip安裝時,The speed has been slow,And it always reports a network error and terminates in the middle of the download.Tried several times without success,終於使用pipThe issue was resolved after replacing the mirror,特此記錄.

問題描述

筆者使用的是MacBook Pro M1version for laptops.之前Pytorch一直不支持M1Chip acceleration,I heard from a classmate recently that this problem seems to be solved.Check out the official website today,確實有了Mac版本的Pytorch.

於是,通過使用PytorchOfficially recommended command,利用pip進行安裝.

(hfai) [email protected] resnet % pip3 install torch torchvision torchaudio
Collecting torch
Downloading torch-1.12.0-cp38-none-macosx_10_9_x86_64.whl (137.6 MB)
━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.2/137.6 MB 290.3 kB/s eta 0:06:31
ERROR: Exception:
Traceback (most recent call last):
File "/Users/jiexing/opt/anaconda3/envs/hfai/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 435, in _error_catcher
yield
……
File "/Users/jiexing/opt/anaconda3/envs/hfai/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 440, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
復制代碼

The result is shown above,Every time I download less than half of the time, I get an error:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

問題排查

經過在網上查找資料,Found out that the problem is due to pipThe default mirror library is abroad,This results in slow and unstable access.而且,由於torchThis library file is relatively large,It increases the risk of network problems caused by file downloads.

解決方法也很簡單,我之前在CSDN發表過一篇關於conda和pipChange the blog of Tsinghua mirror,It is transported here.

 conda使用清華鏡像

首先打開Anaconda Prompt,切換到自己的虛擬環境.之後分別輸入以下三條命令執行:

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/main/
conda config --set show_channel_urls yes
復制代碼

然後在使用condaThe Tsinghua mirror will be used when downloading and installing the package,速度會快很多.比如:

conda install matplotlib=2.2.3
復制代碼

pip使用清華鏡像

1.當次使用

在使用pip installThe command can be followed by parameters-i pypi.tuna.tsinghua.edu.cn/simple,so in this…

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib==2.2.4
復制代碼

But this only works this time,It would be more troublesome to do this every time.

2.永久使用

在Windows環境下,可以直接在當前用戶的目錄(比如我的目錄就是C:\User\Glamour)下新建一個pip目錄,在裡面新建一個pip.ini文件,寫入如下內容:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
復制代碼

保存即可.之後使用pipMuch faster when installing the package.

問題解決

Back to the problem encountered today,我在pip install [name]後加上-i https://pypi.tuna.tsinghua.edu.cn/simpleAfter the parameters are perfectly solved

(hfai) [email protected] resnet % pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
……
Installing collected packages: torch, pillow, torchvision, torchaudio
Successfully installed pillow-9.2.0 torch-1.12.0 torchaudio-0.12.0 torchvision-0.13.0
復制代碼

拓展:其他國內鏡像源

In addition to the Tsinghua mirror source,There are also some well-known mirror sources in China, such as Ali mirror sources,HKUST mirror source, etc,its specific list(不完全統計)如下:

  • 阿裡雲: mirrors.aliyun.com/pypi/simple…
  • 中國科技大學: pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣(douban):pypi.douban.com/simple/

總結

This blog mainly introduces how toconda和pipUse Tsinghua mirror for stabilization,快速的python包安裝,通過使用-iThe parameters can flexibly set the download address of the package,非常方便.最後,This article also gives some other package download addresses other than Tsinghua mirrors that can be used flexibly.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved