python中pip换源

1. 背景

使用pip安装Python,默认镜像源在国内使用一般十分缓慢,我们可以通过换源,来解除下载速度的限制。

2. Python 的 pip 换源

Python 的 pip 可以通过修改配置文件或直接在命令行中使用参数来更换源(即更换 PyPI 的镜像)。

以下是几种常用的方法:

2.1 临时换源(命令行中使用参数)

如果你只想为一次 pip 安装命令换源,可以使用global.index-url 或 -i 参数来指定源,如安装requests库:

pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

2.2 永久换源(修改配置文件)

2.2.1 Windows系统

如果您的操作系统是Windows,请按照下面步骤换源:

  • 在用户目录下创建一个名为 pip 的文件夹(如果尚不存在)。
  • pip 文件夹中创建一个名为 pip.ini 的文件(如果尚不存在)。
  • pip.ini 文件中添加以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

trusted-host 字段是为了信任指定的源,避免在安装时因 SSL 验证问题导致的问题。

2.2.2 Linux/macOS系统

如果您的操作系统是Linux或macOS,请按照下面步骤换源:

  • 在用户的主目录下创建(或修改)一个 .pip 文件夹(如果尚不存在)。
  • .pip 文件夹中创建(或修改)一个名为 pip.conf 的文件(如果尚不存在)。
  • pip.conf 文件中添加与 Windows 用户相同的配置内容。

2.3 使用 pip-config 命令换源(Linux/macOS 特定)

对于 Linux/macOS 用户,你也可以使用 pip 自身的 config 命令来设置配置:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

查看镜像源是否配置成功

pip config list

如果显示如下结果,则表示配置成功:
在这里插入图片描述

3. 常用的 PyPI 镜像源

3.2 清华大学 TUNA

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

3.3 阿里云

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

3.4 华为云

pip config set global.index-url https://pypi.huaweicloud.com/simple/

3.5 中国科技大学

pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/

3.6 豆瓣

pip config set global.index-url https://pypi.douban.com/simple/

3.7 腾讯

pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple

注意,由于网络环境和镜像源维护情况的变化,某些镜像源可能无法正常工作。你可以尝试不同的镜像源,找到最适合你的那个。

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要更新Pythonpip换源,您可以按照以下步骤进行操作: 1. 首先,打开命令行或终端窗口。 2. 使用以下命令安装requests库,该库用于网络请求: pip install requests 3. 使用以下命令更换pip的源为豆瓣源: pip config set global.index-url http://pypi.douban.com/simple 4. 确保您的网络连接正常,然后运行以下命令验证更换是否成功: pip config get global.index-url 此命令应该返回豆瓣源的URL。 注意:您也可以选择其他源来替换,比如V2EX、国科学技术大学、清华等。只需将上述步骤的URL替换为您选择的源的URL即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python编程 pip换源](https://blog.csdn.net/yj11290301/article/details/127197249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [浅析Python3 pip换源问题](https://download.csdn.net/download/weixin_38607784/12856617)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [PythonPIP升级&换源](https://blog.csdn.net/qq_44281295/article/details/127692738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码界领航

你的鼓励将是我最最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值