全局设置Python的pip下载国内镜像源(清华源)_pip 清华源
方法一:通过命令设置(推荐)
bash复制插入
# 设置默认镜像源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple# 添加信任主机(避免SSL验证问题)pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
复制插入
方法二:手动修改配置文件
Windows 系统
- 创建文件:
%USERPROFILE%\\pip\\pip.ini
- 写入内容:
ini复制插入
[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simpletrusted-host = pypi.tuna.tsinghua.edu.cn
复制插入
Linux/macOS 系统
- 创建文件:
~/.pip/pip.conf
- 写入内容:
ini复制插入
[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simpletrusted-host = pypi.tuna.tsinghua.edu.cn
复制插入
验证设置
bash复制插入
pip config list
复制插入
应显示:
复制插入
global.index-url=\'https://pypi.tuna.tsinghua.edu.cn/simple\'global.trusted-host=\'pypi.tuna.tsinghua.edu.cn\'
复制插入
注意事项
- 此设置为永久生效,对所有虚拟环境有效
- 若需临时恢复官方源,安装时添加:
bash复制插入
pip install -i https://pypi.org/simple 包名
复制插入
- 清华源地址需保持最新,可查看清华镜像站帮助文档
设置完成后,所有
pip install
命令将自动使用清华源加速下载,显著提升国内安装速度。
-
或使用阿里云源:
echo -e \"[global]\\nindex-url = https://mirrors.aliyun.com/pypi/simple/\\ntrusted-host = mirrors.aliyun.com\" > ~/.pip/pip.conf
镜像源推荐列表
镜像名称 URL 适用场景 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple
国内首选,更新快 阿里云 https://mirrors.aliyun.com/pypi/simple/
企业级稳定 腾讯云 https://mirrors.cloud.tencent.com/pypi/simple/
云服务优化 完整操作流程
-
在地址栏输入
%APPDATA%
→ 回车 -
进入后:
-
如果已有
pip
文件夹:直接打开 -
如果没有:右键 → 新建 → 文件夹 → 命名为
pip
-
-
在
pip
文件夹内:-
右键 → 新建 → 文本文档 → 重命名为
pip.ini
(注意:需显示文件扩展名才能修改,否则会是pip.ini.txt
)
-
-
用记事本打开
pip.ini
,粘贴以下内容: -
打开VS Code的新终端
-
执行:
-
看不到AppData文件夹?
-
在文件管理器顶部点击\"查看\" → 勾选\"隐藏的项目\"
-
-
修改后无效?
-
检查文件名必须是
pip.ini
(不是.txt) -
关闭所有VS Code窗口重新打开
-
-
想恢复默认源?
直接删除pip.ini
文件即可 -
这是用户级的全局配置
-
所有Python环境(包括F盘的)都会继承这个配置
-
类似浏览器的收藏夹,无论你把浏览器装在哪,收藏夹位置都是固定的
-
pycharm使用阿里源,清华源安装包报错:
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=\'mirro
rs.aliyun.com\', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError(SSLError(1, \'[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)\'),)) - skipping
解决:使用阿里云镜像源 -
安装某个包报错没有这个版本:
WARNING: Discarding http://mirrors.aliyun.com/pypi/packages/c3/4d/d4089e1a3dd25b46bebdb55a992b0797cff657b4477bc32ce28038fdecbc/anyjson-0.3.3.tar.gz# sha256=37812d863c9ad3e35c0734c42e0bf0320ce8c3bed82cd20ad54cb34d158157ba (from http://mirrors.aliyun.com/pypi/simple/anyjson/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. INFO: pip is looking at multiple versions of billiard to determine which version is compatible with other requirements. This could take a while. INFO: pip is looking at multiple versions of celery to determine which version is compatible with other requirements. This could take a while. ERROR: Could not find a version that satisfies the requirement anyjson>=0.3.3 (from kombu) (from versions: 0.1, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.3, 0.3.1, 0.3.2, 0.3.3) ERROR: No matching distribution found for anyjson>=0.3.3
解决:清除缓存:pip cache purge
解决方案:
-
临时降级 setuptools(推荐先尝试):
bash
pip install \"setuptools<58.0.0\"pip install celery==3.1.25
原因:
use_2to3
参数在 setuptools v58 中被移除,降级可解决兼容性问题 -
手动安装修复版 anyjson:
bash
# 先安装其他依赖pip install billiard==3.3.0.23 kombu==3.0.37 amqp==1.4.9# 手动安装修复的anyjsonpip install git+https://github.com/ajdavis/anyjson.git
-
使用替代的 JSON 库:
bash
pip uninstall anyjsonpip install simplejson
然后在代码中添加:
python
import simplejson as anyjson