pip常用镜像源

本文介绍了如何在Python开发中使用国内镜像源加速包的安装,如清华大学、阿里云、中国科大等,以及在Linux环境下通过`requirements.txt`文件管理和信任国外站点的技巧。

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

使用方式,比如使用清华的源:
pip install <package> -i https://pypi.tuna.tsinghua.edu.cn/simple

https://pypi.tuna.tsinghua.edu.cn/simple https://pypi.mirrors.ustc.edu.cn/simple

国内常用镜像源
清华大学 :

https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云:

http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 :

http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:

http://pypi.hustunique.com/
豆瓣源:

http://pypi.douban.com/simple/
腾讯源:

http://mirrors.cloud.tencent.com/pypi/simple
华为镜像源:

https://repo.huaweicloud.com/repository/pypi/simple/

如果使用linux 则先上cd (requirements的文件路径)

pip install <package> -i https://pypi.tuna.tsinghua.edu.cn/simple

pip3 install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

#--trusted-host mirrors.aliyun.com为信任国外站点

pip install 的一些命令

pip install -u 是升级安装
pip install --user 是安装在本用户的目录下
# 这样会将Python 程序包安装到 $HOME/.local 路径下,其中包含三个字文件夹:bin,lib 和 share。

pip install -r <file>
Install from the given requirements file. This option can be used multiple times. 从给定的需求文件安装。 此选项可以多次使用。(可以嵌套使用)

pip install -i <url> or pip install --index-url <url>
指定从某个源安装包,比如pip install xxx -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Python软件包索引的基本URL(默认为https://pypi.org/simple)。 这应指向符合PEP 503(简单存储库API)的存储库或以相同格式布置的本地目录。(软件包存放的URL地址,需要符合PEP 503协议)


pip install --extra-index-url <url>

--trusted-host <hostname> 
比如:--trusted-host    mirrors.aliyun.com,将某个host或者host:port标记为可信赖的,即使它没有有效的HTTPS或任何HTTPS。

pip install --pre 
Include pre-release and development versions. By default, pip only finds stable versions. 查找预发行和正在开发的版本,默认情况下,pip仅查找稳定版本。

# 一个使用例子
pip install --pre xxxx --user -i http://pypi.xxx.cc/hobot-local/simple --extra-index-url=http://mirrors.aliyun.com/pypi/simple/ --trusted-host pypi.xxx.cc --trusted-host mirrors.aliyun.com


 

### 配置 Pip 使用国内镜像源 为了提高 Python 包的下载速度并解决可能遇到的连接问题,可以将 pip 的源更改为国内镜像站点。这可以通过多种方式实现。 #### 单次安装时指定镜像源 对于临时需求,在每次执行 `pip install` 命令的时候通过参数来指定镜像源是一个简单的方法: ```bash pip install --index-url https://mirrors.aliyun.com/pypi/simple some-package ``` 或者使用清华大学开源软件镜像站作为例子: ```bash pip install SomePackage -i https://pypi.tuna.tsinghua.edu.cn/simple/ ``` 这种方式适用于不想更改全局配置的情况[^2]。 #### 设置全局默认镜像源 如果希望永久改变 pip 默认使用的索引服务器,则可以在命令行中设置全局配置项: ```bash pip config set global.index-url <your_mirror_url> ``` 例如,要切换到阿里云提供的 PyPI 镜像服务,可输入如下指令: ```bash pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ ``` 这样以后所有的 pip 安装操作都会自动采用新的镜像源了。 #### 修改用户级别的配置文件 另一种持久化的方式是在用户的 home 目录下创建或编辑 `.pip/pip.conf` 文件(Windows 上可能是 `%APPDATA%\pip\pip.ini`),添加以下内容以指明所选的镜像地址: ```ini [global] index-url = https://mirrors.aliyun.com/pypi/simple/ ``` 这种方法同样能够确保所有后续的 pip 操作都指向设定好的镜像源[^3]。 通过上述任一途径调整 pip 所依赖的资源库位置之后,应该能显著改善包获取的速度与稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值