Python 修改 pip 源为国内源

文章介绍了如何通过临时和永久方式优化Pythonpip包的下载速度,包括临时换源使用-i参数和永久换源修改pip配置文件,以及处理可能出现的信任主机警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.临时换源:

 #清华源
pip install markdown -i https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里源
pip install markdown -i https://mirrors.aliyun.com/pypi/simple/
# 腾讯源
pip install markdown -i http://mirrors.cloud.tencent.com/pypi/simple
# 豆瓣源
pip install markdown -i http://pypi.douban.com/simple/

2.永久换源

# 清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# 腾讯源
pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple
# 豆瓣源
pip config set global.index-url http://pypi.douban.com/simple/
# 换回默认源
pip config unset global.index-url

pip换源的方式

在使用Python安装包工具pip时经常会出现下载很慢的情况,这其中有很大一部分原因和pip的源有关,python的pip是设置的默认源:

https://pypi.org/simple,这个源在国内的下载速度很慢,所以我们为了提高包的下载速度我们可以通过换源来实现。

PYPI国内源路径

换源方式

这里我们提供两种换源的方式:

  1. 临时换源
  2. 永久换源
临时换源

临时换源只需要在pip安装包时,加上一个-i参数后接源的url即可:

# 下载python中的numpy库,这里使用的是豆瓣源
pip install numpy -i http://pypi.douban.com/simple 

显然不是一个一劳永逸的方法,只有下少量包的时候有使用的场景,下面我要介绍永久换源的方法,通过这个方式换源,那么以后我们下载的包就可以全部从这个url中下载了,这样大大减轻了我们的工作量,明显比临时换源的方法更好。

永久换源(更换默认源)
Linux
  1. 在根目录下创建/修改~/.pip/pip.confpip配置文件;

  2. 进入文件新增/修改内容;

    [global]
    index-url=http://pypi.douban.com/simple
    [install]
    trusted-host=pypi.douban.com
    
  3. 保存文件并退出;

Windows
  1. windows在%HOMEPATH%\pip\pip.ini中修改上面第二步的内容;(例如:C:\Users\hp\AppData\Roaming\pip\pip.ini)
  2. 保存文件退出;

常见问题

  • 安装包的时候出现

    Collecting beautifulsoup4
    The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with ‘–trusted-host mirrors.aliyun.com’.
    Could not find a version that satisfies the requirement beautifulsoup4 (from versions: )
    No matching distribution found for beautifulsoup4
    

    这是一个问题是在pip镜像升级报警,只需要添加信任源即可:

    • 临时换源处理

      pip install beautifulsoup4 --trusted-host mirrors.aliyun.com
      
    • 更换默认源配置

      [install]
      trusted-host=pypi.douban.com
### 配置Python pip使用国内镜像 为了提高下载速度并解决可能遇到的网络问题,可以配置`pip`使用国内镜像。以下是具体方法: #### 通过命令行临时指定镜像 可以在每次执行安装操作时,在命令后面加上参数来指定使用的镜像地址。 对于阿里云镜像: ```bash pip install SomePackage -i http://mirrors.aliyun.com/pypi/simple/ ``` 针对清华大学镜像,则应改为如下形式: ```bash pip install SomePackage -i https://pypi.tuna.tsinghua.edu.cn/simple/ ``` #### 设置全局默认镜像 ##### Windows平台设置方式之一——编辑或新建配置文件 访问路径 `C:\Users\ 用户名 \AppData\Roaming\pip\pip.ini` 创建或修改该位置下的`pip.ini`文档[^2],向其中加入以下内容以指明新的索引URL以及信任主机列表。 ```ini [global] index-url=https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com ``` ##### Linux/MacOS平台设置方式之一——编辑或新建配置文件 找到位于家目录中的`.pip`文件夹内的`pip.conf`文件(如果不存在则需自行建立),按照上述Windows部分给出的例子填写相应字段即可实现相同效果。 另外一种适用于所有操作系统的方法就是利用`pip config`子命令来进行设定,这同样能够达到持久化更改的目的。例如要切换至中科大镜像可输入下面这条语句完成设置: ```bash pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/ pip config set global.trusted-host pypi.mirrors.ustc.edu.cn ``` 这样以后再调用`pip install`的时候就会自动采用所选的国内镜像站点作为数据获取渠道了[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值