『技术随手学』解决 pip conda install 网络故障中断

时间:2022-07-24
本文章向大家介绍『技术随手学』解决 pip conda install 网络故障中断,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

我也想能把你照亮,在你的生命中留下阳光

0.引子

在用pip或者conda安装一些包时有时会因为网络原因导致下载失败,进而无法安装。一般的解决方法就是换源,或者重复安装。

在本篇文章,将使用一个简单高效的方式来解决这个问题。整体思路很简单,把包下载到本地,在使用本地包进行安装。

这样有什么好处呢?其实这解决了直接使用安装时网络不通畅导致中断,又要重新开始下载的问题。因为很多下载工作都支持断点续传,不用重复操作。

1.实战

例子一:conda

当直接安装时:

conda install pytorch=1.4  torchvision cudatoolkit=10.0 cudnn=7 -y

输出:

The following packages will be SUPERSEDED by a higher-priority channel:

  pytorch            anaconda/pkgs/main::pytorch-1.5.0-cpu~ --> anaconda/cloud//pytorch::pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0



Downloading and Extracting Packages
pytorch-1.4.0        | 422.7 MB  | ####################################################3                                                                                                                                                           |  25% 

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud//pytorch/linux-64/pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

可以看出是网络故障导致下载安装失败。细心的小伙伴同时也可以看到一个很有用信息:

CondaHTTPError: HTTP 000 CONNECTION FAILED for url < https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud//pytorch/linux-64/pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2>

这时可以把此链接拷贝到浏览器中下载到本地。笔者习惯使用wget:

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud//pytorch/linux-64/pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2

输出:

Saving to: 'pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2'

pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2          100%[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=========================================================>] 422.70M  2.46MB/s    in 80s     

2020-09-10 03:58:45 (2.06 MB/s) - 'pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2' saved [443230931/443230931]

下载完后,仅需使用conda本地安装指令即可:conda install --use-local

conda install --use-local pytorch-1.4.0-py3.7_cuda10.0.130_cudnn7.6.3_0.tar.bz2

输出:

Downloading and Extracting Packages
################################################################################################################################################################################################################################################## | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

例子二:pip

使用方法和conda类似,先直接安装一个包

pip install tensorflow==2.3

输出:

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting tensorflow==2.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/16/89/f2d29c2eafc2eeafb17d5634340e06366af904d332341200a49d954bce85/tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl (320.4 MB)
     |▏                               | 1.4 MB 1.7 MB/s eta 0:03:06^C
ERROR: Operation cancelled by user

关键信息如下:

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/16/89/f2d29c2eafc2eeafb17d5634340e06366af904d332341200a49d954bce85/tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl (320.4 MB

下载到本地方法如例子一

wget https://pypi.tuna.tsinghua.edu.cn/packages/16/89/f2d29c2eafc2eeafb17d5634340e06366af904d332341200a49d954bce85/tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl

输出:

--2020-09-10 04:11:44--  https://pypi.tuna.tsinghua.edu.cn/packages/16/89/f2d29c2eafc2eeafb17d5634340e06366af904d332341200a49d954bce85/tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Resolving pypi.tuna.tsinghua.edu.cn (pypi.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to pypi.tuna.tsinghua.edu.cn (pypi.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 320368291 (306M) [application/octet-stream]
Saving to: 'tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl'

tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl           100%[==================================================================================================================================================>] 305.53M  3.68MB/s    in 2m 48s  

2020-09-10 04:14:33 (1.82 MB/s) - 'tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl' saved [320368291/320368291]

下载完成后,使用 pip install 即可

pip install tensorflow-2.3.0-cp37-cp37m-manylinux2010_x86_64.whl