文章目录
- 在 pip install 命令中通过 -i 参数指定镜像源: pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/ # 阿里云 pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple # 清华大学 pip install 包名 -i https://pypi.doubanio.com/simple/ # 豆瓣
- 创建或修改 pip 配置文件: # Linux/macOS:创建配置目录 mkdir -p ~/.pip # 编辑配置文件(若不存在会自动创建) nano ~/.pip/pip.conf 在配置文件中添加以下内容(以阿里云为例): [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com # 信任该镜像源,避免 SSL 警告 保存后,所有 pip install 命令都会默认使用该镜像源。
- 镜像源 URL 阿里云 https://mirrors.aliyun.com/pypi/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple 中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣 https://pypi.doubanio.com/simple/
- # 添加清华镜像 conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ # 添加 conda-forge 社区源(可选,包含更多包) conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ # 设置搜索时显示通道地址 conda config –set show_channel_urls yes
- conda config –show channels 输出应类似: channels: – https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ – https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ – https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ – defaults
- conda config –remove-key channels
目录
- 一、pip 更换镜像源
- 1. 临时使用镜像源(单次命令)
- 2. 永久配置镜像源
- 3. 常用国内镜像源地址
- 二、conda 更换镜像源(适用于 Anaconda/Miniconda)
- 1. 添加镜像源
- 2. 查看配置结果
- 3. 恢复默认源
- 三、验证镜像源是否生效
- 1. pip 验证
- 2. conda 验证
- 四、注意事项
在 Python 中切换镜像源主要涉及 pip 包管理器 和 conda 环境(如 Anaconda、Miniconda) 的配置。国内访问 Python 官方源(PyPI)可能较慢,因此推荐使用国内镜像源(如阿里云、清华大学、豆瓣等)。以下是具体切换方法:
在 pip install 命令中通过 -i 参数指定镜像源:
pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/ # 阿里云 pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple # 清华大学 pip install 包名 -i https://pypi.doubanio.com/simple/ # 豆瓣
创建或修改 pip 配置文件:
# Linux/macOS:创建配置目录 mkdir -p ~/.pip # 编辑配置文件(若不存在会自动创建) nano ~/.pip/pip.conf
在配置文件中添加以下内容(以阿里云为例):
[global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com # 信任该镜像源,避免 SSL 警告
保存后,所有 pip install 命令都会默认使用该镜像源。
| 镜像源 | URL |
|---|---|
| 阿里云 | https://mirrors.aliyun.com/pypi/simple/ |
| 清华大学 | https://pypi.tuna.tsinghua.edu.cn/simple |
| 中国科学技术大学 | https://pypi.mirrors.ustc.edu.cn/simple/ |
| 豆瓣 | https://pypi.doubanio.com/simple/ |
# 添加清华镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
# 添加 conda-forge 社区源(可选,包含更多包)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
conda config --show channels
输出应类似:
channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ - defaults
conda config --remove-key channels
pip install -vvv 包名 2>&1 | grep "Fetching" # 查看下载地址是否为镜像源
conda install 包名 # 安装时观察下载地址
- 镜像同步延迟:国内镜像会定期同步 PyPI 官方源,但可能存在数小时的延迟。若遇到“找不到包”的问题,可临时切换回官方源。
- 虚拟环境独立配置:若使用虚拟环境(如
venv、virtualenv),配置文件路径可能不同(如 ~/.virtualenvs/环境名/pip.conf)。
- 优先使用官方源:若安装特定版本的包遇到问题,可尝试使用官方源:
pip install 包名 -i https://pypi.org/simple
venv、virtualenv),配置文件路径可能不同(如 ~/.virtualenvs/环境名/pip.conf)。pip install 包名 -i https://pypi.org/simple
到此这篇关于Python中切换镜像源的几种实现方法的文章就介绍到这了,更多相关Python 切换镜像源内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python使用pip通过命令设置国内镜像源的三种方式
- 使用清华大学镜像源安装Python库的详细教程
- Python入门之pip永久镜像源的配置方法
- Python轻松更换国内镜像源的三种方法推荐
- python修改镜像源的方法步骤
- Python pip更换镜像源问题及解决
- Python pip更换镜像源的流程步骤
- Python基础之pip如何更换镜像源
- Python Anaconda以及Pip配置清华镜像源代码示例
- python更换国内镜像源三种实用方法
- Python PiP 换镜像源的实现步骤