381 字
2 分钟
pip永久换源和临时换源的方法
2023-06-06

为什么要换源

因为在pip下载python包时由于服务器架设在国外这样国内访问掉包率和速度会很慢

后一种还好,前一种下载体积大的包不可避免会超时,这样就要用到国内的镜像源了

第一种(非永久改源)

这一种只需要在下载的包名后加一个“-i 镜像链接

下面是几个常用的镜像

清华大学:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:https://mirrors.aliyun.com/pypi/simple

中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple

豆瓣:https://pypi.douban.com/simple

那么命令就是

pip install 包名 -i https://mirrors.aliyun.com/pypi/simple

第二种方法(永久改源)

在“C:\Users\%username%\AppData\Roaming”目录下(或者直接在地址栏输入%username%

创建一个pip.ini

输入以下命令

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

其中链接index-urltrusted-host可以改为其他的镜像源

最好重启一下

应该就可以了

一些改源时候会遇到的问题

一部分的pip源链接可能是http格式的(不是https格式)这样下载时可能会出警告甚至报错

ERROR: Could not find a version that satisfies the requirement pandas (from versions: none)
ERROR: No matching distribution found for pandas
 

注意改为https格式的链接就行

部分镜像源的部分包的部分老版本可能会没有,如果下载不到相应版本还是换成官方的镜像源下载吧。

pip永久换源和临时换源的方法
https://fuwari.vercel.app/posts/pip永久换源和临时换源的方法/
作者
木木em哈哈
发布于
2023-06-06
许可协议
CC BY-NC-SA 4.0