pip安装库时出现Read timed out.解决办法

在安装python库,在网络不好的情况会遇到

raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.


解决办法1:延长timeout时间

raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.


pip --default-timeout=100 install Package

将Package替换你所需要的库就行


解决办法2:换源,将pip源更换到国内镜像

    #例如安装scipy时使用豆瓣的源
    pip install --index-url https://pypi.douban.com/simple scipy
    #使用清华大学的源
    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple –upgrade tensorflow-gpu

将pip源更换到国内镜像
用pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。
其中,比较常用的国内镜像包括:

(1)阿里云 http://mirrors.aliyun.com/pypi/simple/
(2)豆瓣http://pypi.douban.com/simple/
(3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
(4)中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
(5)华中科技大学http://pypi.hustunique.com/

(6)网易 http://mirrors.163.com/pypi/simple/

注意:新版ubuntu要求使用https源。

设置方法:(以清华镜像为例,其它镜像同理)
(1)临时使用:
可以在使用pip的时候,加上参数-i和镜像地址(如
https://pypi.tuna.tsinghua.edu.cn/simple),
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas,这样就会从清华镜像安装pandas库。

(2)永久修改,一劳永逸:
(a)Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:

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

(b) windows下,直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,然后新建文件pip.ini,即 %HOMEPATH%\pip\pip.ini,在pip.ini文件中输入以下内容(以豆瓣镜像为例):

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

创建pip.ini文件可以用文本编辑器,例如 sublime text3。

anaconda conda 切换为国内源

windows下

在清华源和中科大源之间自行选择

1 添加清华源

命令行中直接使用以下命令

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

# 设置搜索时显示通道地址
conda config --set show_channel_urls yes

注意如果需要pytorch, 还需要添加pytorch的镜像

2 添加中科大源

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes

3 切换回默认源

conda config --remove-key channels

Linux下

将以上配置文件写在~/.condarc
vim ~/.condarc

channels:
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true

其他

使用如下命令可以查看库的各个版本及平台

anaconda search -t conda tensorflow

 

anaconda show 包名, 可以查看该包名的安装命令 

anaconda show anaconda/tensorflow-gpu

<think>根据用户提供的站内引用和当前问题,用户希望解决pip安装transformers出现的“Readtimedout”问题。超通常是由于网络连接不稳定或PyPI服务器响应慢导致的。参考引用[1]、[2]、[3]提供了相关解决方案:-引用[1]和[2]建议使用国内镜像源加速下载(如豆瓣源、清华源)-引用[3]明确提到两种方法:增加超间和更换镜像源-引用[4]和[5]虽然提供了其他安装方式(如源码安装、指定版本),但更适用于解决兼容性问题而非超问题用户当前的问题是网络超,因此重点采用引用[1][2][3]的方案:1.更换国内PyPI镜像源(优先级最高)2.增加超间参数3.特定版本指定(版本兼容性作为备选)在回答中需注意:-明确标注引用来源(如[^2][^3])-保持LaTeX格式规范(但当前问题不涉及数学公式)-提供可执行的完整命令示例-补充原理说明让用户理解为什么有效生成相关问题需围绕pip安装的扩展场景</think> 针对pip安装transformers出现"Read timed out"的问题,以下是具体解决方案,参考了您提供的站内引用: ### 解决方案 1. **使用国内镜像源加速下载**(最有效方案) ```bash pip install -i https://pypi.tuna.tsinghua.edu.cn/simple transformers --trusted-host pypi.tuna.tsinghua.edu.cn ``` 或使用豆瓣源: ```bash pip install --index-url https://pypi.douban.com/simple transformers ``` *原理*:国内镜像源服务器延迟更低,避免国际网络波动导致超[^2][^1] 2. **增加超间参数** ```bash pip install --default-timeout=300 transformers ``` *说明*:将默认超间从60秒延长至300秒,适应不稳定网络[^3] 3. **指定兼容版本安装** ```bash pip install transformers==4.30.2 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` *注意*:某些版本文件较小,下载更稳定[^2] 4. **前置依赖安装**(解决间接超) ```bash pip install torch==1.5.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html ``` *原理*:先安装基础依赖,避免transformers安装连带下载大文件[^5] ### 其他方案 - **源码安装**(适用于持续超) ```bash git clone https://github.com/huggingface/transformers cd transformers && pip install -e . ``` 避开PyPI直接安装[^4] - **分段下载** ```bash pip download transformers -d ./packages pip install --no-index --find-links=./packages transformers ``` 先下载后离线安装 > **超原因**:主要因国际网络不稳定或文件较大(transformers基础包约15MB,含依赖可达100+MB)。建议优先使用镜像源+超参数组合方案[^1][^2][^3]。
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值